Example usage for java.io FilterInputStream read

List of usage examples for java.io FilterInputStream read

Introduction

In this page you can find the example usage for java.io FilterInputStream read.

Prototype

public int read(byte b[], int off, int len) throws IOException 

Source Link

Document

Reads up to len bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    byte[] buffer = new byte[6];

    InputStream is = new FileInputStream("C://test.txt");
    FilterInputStream fis = new BufferedInputStream(is);

    // returns number of bytes read to buffer
    int i = fis.read(buffer, 2, 4);

    System.out.println("Number of bytes read: " + i);

    // for each byte in buffer
    for (byte b : buffer) {
        // converts byte to character
        char c = (char) b;

        // if byte is null
        if (b == 0) {
            c = '-';
        }/*from ww  w  . j a  va  2s  .c  o m*/
        System.out.println("Char read from buffer b: " + c);
    }

}

From source file:org.adblockplus.brazil.RequestHandler.java

@Override
public boolean respond(final Request request) throws IOException {
    boolean block = false;

    final String referrer = request.getRequestHeader("referer");
    try {/*w ww  .j  av  a2s . c  o m*/
        block = application.matches(request.url, request.query, referrer, request.getRequestHeader("accept"));
    } catch (final Exception e) {
        Log.e(prefix, "Filter error", e);
    }

    request.log(Server.LOG_LOG, prefix, block + ": " + request.url);

    int count = request.server.requestCount;
    if (shouldLogHeaders) {
        // FIXME Don't log to "err"
        System.err.println(dumpHeaders(count, request, request.headers, true));
    }

    if (block) {
        request.sendHeaders(204, null, 0);
        BLOCKED_REQUESTS.incrementAndGet();
        return true;
    }

    UNBLOCKED_REQUESTS.incrementAndGet();

    // Do not further process non-http requests
    if (!RE_HTTP.matcher(request.url).find()) {
        return false;
    }

    String url = request.url;

    if ((request.query != null) && (request.query.length() > 0)) {
        url += "?" + request.query;
    }

    /*
     * "Proxy-Connection" may be used (instead of just "Connection")
     * to keep alive a connection between a client and this proxy.
     */
    final String pc = request.headers.get("Proxy-Connection");
    if (pc != null) {
        request.connectionHeader = "Proxy-Connection";
        request.keepAlive = pc.equalsIgnoreCase("Keep-Alive");
    }

    HttpRequest.removePointToPointHeaders(request.headers, false);

    final HttpRequest target = new HttpRequest(url);
    try {
        target.setMethod(request.method);
        request.headers.copyTo(target.requestHeaders);

        if (proxyHost != null) {
            target.setProxy(proxyHost, proxyPort);
            if (auth != null) {
                target.requestHeaders.add("Proxy-Authorization", auth);
            }
        }

        if (request.postData != null) {
            final OutputStream out = target.getOutputStream();
            out.write(request.postData);
            out.close();
        } else {
            target.setHttpInputStream(request.in);
        }
        target.connect();

        if (shouldLogHeaders) {
            System.err.println("      " + target.status + "\n"
                    + dumpHeaders(count, request, target.responseHeaders, false));
        }
        HttpRequest.removePointToPointHeaders(target.responseHeaders, true);

        request.setStatus(target.getResponseCode());
        target.responseHeaders.copyTo(request.responseHeaders);
        try {
            request.responseHeaders.add("Via", target.status.substring(0, 8) + via);
        } catch (final StringIndexOutOfBoundsException e) {
            request.responseHeaders.add("Via", via);
        }

        // Detect if we need to add ElemHide filters
        final String type = request.responseHeaders.get("Content-Type");

        String[] selectors = null;
        if (type != null && type.toLowerCase().startsWith("text/html")) {
            String reqHost = "";

            try {
                reqHost = (new URL(request.url)).getHost();
            } catch (final MalformedURLException e) {
                // We are transparent, it's not our deal if it's malformed.
            }

            selectors = application.getSelectorsForDomain(reqHost, referrer);
        }
        // If no filters are applicable just pass through the response
        if (selectors == null || target.getResponseCode() != 200) {
            final int contentLength = target.getContentLength();
            if (contentLength == 0) {
                // we do not use request.sendResponse to avoid arbitrary
                // 200 -> 204 response code conversion
                request.sendHeaders(-1, null, -1);
            } else {
                request.sendResponse(target.getInputStream(), contentLength, null, -1);
            }
        }
        // Insert filters otherwise
        else {
            final HttpInputStream his = target.getInputStream();
            int size = target.getContentLength();
            if (size < 0) {
                size = Integer.MAX_VALUE;
            }

            FilterInputStream in = null;
            FilterOutputStream out = null;

            // Detect if content needs decoding
            String encodingHeader = request.responseHeaders.get("Content-Encoding");
            if (encodingHeader != null) {
                encodingHeader = encodingHeader.toLowerCase();
                if (encodingHeader.equals("gzip") || encodingHeader.equals("x-gzip")) {
                    in = new GZIPInputStream(his);
                } else if (encodingHeader.equals("compress") || encodingHeader.equals("x-compress")) {
                    in = new InflaterInputStream(his);
                } else {
                    // Unsupported encoding, proxy content as-is
                    in = his;
                    out = request.out;
                    selectors = null;
                }
            } else {
                in = his;
            }
            // Use chunked encoding when injecting filters in page
            if (out == null) {
                request.responseHeaders.remove("Content-Length");
                request.responseHeaders.remove("Content-Encoding");
                out = new ChunkedOutputStream(request.out);
                request.responseHeaders.add("Transfer-Encoding", "chunked");
                size = Integer.MAX_VALUE;
            }

            String charsetName = "utf-8";
            final String contentType = request.responseHeaders.get("Content-Type");
            if (contentType != null) {
                final Matcher matcher = Pattern.compile("charset=([^;]*)").matcher(contentType);
                if (matcher.matches()) {
                    try {
                        final String extractedCharsetName = matcher.group(0);
                        Charset.forName(extractedCharsetName);
                        charsetName = extractedCharsetName;
                    } catch (final IllegalArgumentException e) {
                        Log.e(prefix, "Unsupported site charset, falling back to " + charsetName, e);
                    }
                }
            }

            request.sendHeaders(-1, null, -1);

            final byte[] buf = new byte[Math.min(4096, size)];

            boolean sent = selectors == null;
            final BoyerMoore matcher = new BoyerMoore("<html".getBytes());

            while (size > 0) {
                out.flush();

                count = in.read(buf, 0, Math.min(buf.length, size));
                if (count < 0) {
                    break;
                }
                size -= count;
                try {
                    // Search for <html> tag
                    if (!sent && count > 0) {
                        final List<Integer> matches = matcher.match(buf, 0, count);
                        if (!matches.isEmpty()) {
                            // Add filters right before match
                            final int m = matches.get(0);
                            out.write(buf, 0, m);
                            out.write("<style type=\"text/css\">\n".getBytes());
                            out.write(StringUtils.join(selectors, ",\r\n").getBytes(charsetName));
                            out.write("{ display: none !important }</style>\n".getBytes());
                            out.write(buf, m, count - m);
                            sent = true;
                            continue;
                        }
                    }
                    out.write(buf, 0, count);
                } catch (final IOException e) {
                    break;
                }
            }
            // The correct way would be to close ChunkedOutputStream
            // but we can not do it because underlying output stream is
            // used later in caller code. So we use this ugly hack:
            if (out instanceof ChunkedOutputStream)
                ((ChunkedOutputStream) out).writeFinalChunk();
        }
    } catch (final InterruptedIOException e) {
        /*
         * Read timeout while reading from the remote side. We use a
         * read timeout in case the target never responds.
         */
        request.sendError(408, "Timeout / No response");
    } catch (final EOFException e) {
        request.sendError(500, "No response");
    } catch (final UnknownHostException e) {
        request.sendError(500, "Unknown host");
    } catch (final ConnectException e) {
        request.sendError(500, "Connection refused");
    } catch (final IOException e) {
        /*
         * An IOException will happen if we can't communicate with the
         * target or the client. Rather than attempting to discriminate,
         * just send an error message to the client, and let the send
         * fail if the client was the one that was in error.
         */

        String msg = "Error from proxy";
        if (e.getMessage() != null) {
            msg += ": " + e.getMessage();
        }
        request.sendError(500, msg);
        Log.e(prefix, msg, e);
    } finally {
        target.close();
    }
    return true;
}