Search in sources :

Example 81 with BufferedInputStream

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream)

Example 82 with BufferedInputStream

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);
}
Also used : PacketLineIn(org.eclipse.jgit.transport.PacketLineIn) BufferedInputStream(java.io.BufferedInputStream) SafeBufferedOutputStream(org.eclipse.jgit.util.io.SafeBufferedOutputStream)

Example 83 with BufferedInputStream

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;
}
Also used : Context(android.content.Context) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 84 with BufferedInputStream

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;
}
Also used : SocketException(java.net.SocketException) BufferedInputStream(java.io.BufferedInputStream) EOFException(java.io.EOFException)

Example 85 with BufferedInputStream

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()");
}
Also used : SocketException(java.net.SocketException) BufferedInputStream(java.io.BufferedInputStream) ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1700 FileInputStream (java.io.FileInputStream)854 IOException (java.io.IOException)836 InputStream (java.io.InputStream)707 File (java.io.File)449 BufferedOutputStream (java.io.BufferedOutputStream)228 FileOutputStream (java.io.FileOutputStream)218 DataInputStream (java.io.DataInputStream)168 ByteArrayInputStream (java.io.ByteArrayInputStream)159 FileNotFoundException (java.io.FileNotFoundException)147 URL (java.net.URL)147 ZipEntry (java.util.zip.ZipEntry)123 ByteArrayOutputStream (java.io.ByteArrayOutputStream)112 OutputStream (java.io.OutputStream)100 ZipInputStream (java.util.zip.ZipInputStream)72 GZIPInputStream (java.util.zip.GZIPInputStream)68 ArrayList (java.util.ArrayList)62 HashMap (java.util.HashMap)62 HttpURLConnection (java.net.HttpURLConnection)61 ObjectInputStream (java.io.ObjectInputStream)56