use of java.io.BufferedInputStream in project facebook-android-sdk by facebook.
the class Utility method readStreamToString.
public static String readStreamToString(InputStream inputStream) throws IOException {
BufferedInputStream bufferedInputStream = null;
InputStreamReader reader = null;
try {
bufferedInputStream = new BufferedInputStream(inputStream);
reader = new InputStreamReader(bufferedInputStream);
StringBuilder stringBuilder = new StringBuilder();
final int bufferSize = 1024 * 2;
char[] buffer = new char[bufferSize];
int n = 0;
while ((n = reader.read(buffer)) != -1) {
stringBuilder.append(buffer, 0, n);
}
return stringBuilder.toString();
} finally {
closeQuietly(bufferedInputStream);
closeQuietly(reader);
}
}
use of java.io.BufferedInputStream in project gitblit by gitblit.
the class GitDaemonClient method execute.
void execute(final Socket sock) throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
rawIn = new BufferedInputStream(sock.getInputStream());
rawOut = new SafeBufferedOutputStream(sock.getOutputStream());
if (0 < daemon.getTimeout())
sock.setSoTimeout(daemon.getTimeout() * 1000);
String cmd = new PacketLineIn(rawIn).readStringRaw();
final int nul = cmd.indexOf('\0');
if (nul >= 0) {
// Newer clients hide a "host" header behind this byte.
// Currently we don't use it for anything, so we ignore
// this portion of the command.
//
cmd = cmd.substring(0, nul);
}
final GitDaemonService srv = getDaemon().matchService(cmd);
if (srv == null)
return;
sock.setSoTimeout(0);
srv.execute(this, cmd);
}
use of java.io.BufferedInputStream in project facebook-android-sdk by facebook.
the class AppEventStore method readAndClearStore.
// Only call from singleThreadExecutor
public static synchronized PersistedEvents readAndClearStore() {
AppEventUtility.assertIsNotMainThread();
MovedClassObjectInputStream ois = null;
PersistedEvents persistedEvents = null;
Context context = FacebookSdk.getApplicationContext();
try {
InputStream is = context.openFileInput(PERSISTED_EVENTS_FILENAME);
ois = new MovedClassObjectInputStream(new BufferedInputStream(is));
persistedEvents = (PersistedEvents) ois.readObject();
} catch (FileNotFoundException e) {
// Expected if we never persisted any events.
} catch (Exception e) {
Log.w(TAG, "Got unexpected exception while reading events: ", e);
} finally {
Utility.closeQuietly(ois);
try {
// Note: We delete the store before we store the events; this means we'd
// prefer to lose some events in the case of exception rather than
// potentially log them twice.
// Always delete this file after the above try catch to recover from read
// errors.
context.getFileStreamPath(PERSISTED_EVENTS_FILENAME).delete();
} catch (Exception ex) {
Log.w(TAG, "Got unexpected exception when removing events file: ", ex);
}
}
if (persistedEvents == null) {
persistedEvents = new PersistedEvents();
}
return persistedEvents;
}
use of java.io.BufferedInputStream in project fqrouter by fqrouter.
the class DNSConnection method receive.
/**
* NOTE: If !wait and no message received yet then result == null.
* InterruptedIOException and EOFException may be thrown (only if
* wait is true). Connection remains valid even if IOException is
* thrown. Must be synchronized outside.
*/
public byte[] receive(boolean wait) throws IOException {
byte[] msgBytes;
int msgLen, len;
BufferedInputStream in;
if ((in = this.in) == null)
throw new SocketException("Connection closed");
if ((msgLen = this.msgLen) <= 0)
msgLen = 0;
if ((msgBytes = this.msgBytes) == null) {
do {
if (!wait && in.available() <= 0)
return null;
else if ((len = in.read()) < 0)
throw new EOFException();
else if (msgLen <= 0)
this.msgLen = msgLen = len + 1;
else
break;
} while (true);
if ((msgLen = ((msgLen - 1) << 8) | len) <= 0)
msgLen = 0;
this.msgBytes = msgBytes = new byte[msgLen];
msgLen = 0;
}
for (int avail; (len = msgBytes.length - (this.msgLen = msgLen)) > 0; msgLen += len) if (!wait && (avail = in.available()) < len && (len = avail) <= 0)
return null;
else if ((len = in.read(msgBytes, msgLen, len)) < 0)
throw new EOFException();
this.msgBytes = null;
this.msgLen = 0;
return msgBytes;
}
use of java.io.BufferedInputStream in project fqrouter by fqrouter.
the class DNSConnection method openIncoming.
/**
* NOTE: old connection should be closed. Wait for any incoming
* connection and accept it. If listening is not active or if
* SecurityException is caught then SocketException is thrown. If
* waiting fails then InterruptedIOException is thrown. Must be
* synchronized outside.
**
* @since 3.0
*/
public void openIncoming() throws IOException {
ServerSocket curListener;
if ((curListener = listener) != null) {
try {
Socket socket = curListener.accept();
BufferedInputStream in = new BufferedInputStream(socket.getInputStream(), DNSMsgHeader.UDP_PACKET_LEN);
this.out = socket.getOutputStream();
this.in = in;
this.msgBytes = null;
this.socket = socket;
return;
} catch (SecurityException e) {
}
}
throw new SocketException(curListener == null ? "Not listening" : "SecurityException: accept()");
}
Aggregations