Search in sources :

Example 46 with PushbackInputStream

use of java.io.PushbackInputStream in project jvarkit by lindenb.

the class IOUtils method tryBGZIP.

private static InputStream tryBGZIP(final InputStream in) throws IOException {
    final byte[] buffer = new byte[BlockCompressedStreamConstants.GZIP_BLOCK_PREAMBLE.length];
    final PushbackInputStream push_back = new PushbackInputStream(in, buffer.length + 10);
    int nReads = push_back.read(buffer);
    push_back.unread(buffer, 0, nReads);
    try {
        if (nReads >= buffer.length && buffer[0] == BlockCompressedStreamConstants.GZIP_ID1 && buffer[1] == (byte) BlockCompressedStreamConstants.GZIP_ID2 && buffer[2] == BlockCompressedStreamConstants.GZIP_CM_DEFLATE && buffer[3] == BlockCompressedStreamConstants.GZIP_FLG && buffer[8] == BlockCompressedStreamConstants.GZIP_XFL) {
            return new BlockCompressedInputStream(push_back);
        }
    } catch (final Exception err) {
    // not bzip
    }
    return new GZIPInputStream(push_back);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) PushbackInputStream(java.io.PushbackInputStream) BlockCompressedInputStream(htsjdk.samtools.util.BlockCompressedInputStream) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOException(java.io.IOException)

Example 47 with PushbackInputStream

use of java.io.PushbackInputStream in project camunda-bpm-platform by camunda.

the class EmptyBodyFilter method doFilter.

@Override
public void doFilter(final ServletRequest req, final ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    final boolean isContentTypeJson = CONTENT_TYPE_JSON_PATTERN.matcher(req.getContentType() == null ? "" : req.getContentType()).find();
    if (isContentTypeJson) {
        final PushbackInputStream requestBody = new PushbackInputStream(req.getInputStream());
        int firstByte = requestBody.read();
        final boolean isBodyEmpty = firstByte == -1;
        requestBody.unread(firstByte);
        HttpServletRequestWrapper wrappedRequest = new HttpServletRequestWrapper((HttpServletRequest) req) {

            @Override
            public ServletInputStream getInputStream() throws IOException {
                return new ServletInputStream() {

                    InputStream inputStream = isBodyEmpty ? new ByteArrayInputStream("{}".getBytes(Charset.forName("UTF-8"))) : requestBody;

                    @Override
                    public int read() throws IOException {
                        return inputStream.read();
                    }

                    @Override
                    public int available() throws IOException {
                        return inputStream.available();
                    }

                    @Override
                    public void close() throws IOException {
                        inputStream.close();
                    }

                    @Override
                    public synchronized void mark(int readlimit) {
                        inputStream.mark(readlimit);
                    }

                    @Override
                    public synchronized void reset() throws IOException {
                        inputStream.reset();
                    }

                    @Override
                    public boolean markSupported() {
                        return inputStream.markSupported();
                    }
                };
            }

            @Override
            public BufferedReader getReader() throws IOException {
                return new BufferedReader(new InputStreamReader(this.getInputStream()));
            }
        };
        chain.doFilter(wrappedRequest, resp);
    } else {
        chain.doFilter(req, resp);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PushbackInputStream(java.io.PushbackInputStream) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader)

Example 48 with PushbackInputStream

use of java.io.PushbackInputStream in project endpoints-java by cloudendpoints.

the class IoUtil method getRequestInputStream.

/**
 * Gets an uncompressed {@link InputStream} for the request. If the request specifies gzip
 * encoding, tries to wrap the request input stream in a {@link GZIPInputStream}. If the input
 * stream does not start with a gzip header, then a stream representing a plaintext request is
 * returned.
 */
public static InputStream getRequestInputStream(HttpServletRequest request) throws IOException {
    InputStream bodyStream = request.getInputStream();
    if (bodyStream != null && GZIP_ENCODING.equals(request.getHeader(HEADER_CONTENT_ENCODING))) {
        PushbackInputStream pushbackStream = new PushbackInputStream(bodyStream, 2);
        byte[] header = new byte[2];
        int len = pushbackStream.read(header);
        if (len > 0) {
            pushbackStream.unread(header, 0, len);
        }
        return isGzipHeader(header) ? new GZIPInputStream(pushbackStream) : pushbackStream;
    }
    return bodyStream;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) PushbackInputStream(java.io.PushbackInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream)

Example 49 with PushbackInputStream

use of java.io.PushbackInputStream in project shattered-pixel-dungeon-gdx by 00-Evan.

the class Bundle method read.

public static Bundle read(InputStream stream) throws IOException {
    try {
        BufferedReader reader;
        // determines if we're reading a regular, or compressed file
        PushbackInputStream pb = new PushbackInputStream(stream, 2);
        byte[] header = new byte[2];
        pb.unread(header, 0, pb.read(header));
        // GZIP header is 0x1f8b
        if (header[0] == (byte) 0x1f && header[1] == (byte) 0x8b)
            reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(pb, GZIP_BUFFER)));
        else
            reader = new BufferedReader(new InputStreamReader(pb));
        JSONObject json = (JSONObject) new JSONTokener(reader.readLine()).nextValue();
        reader.close();
        return new Bundle(json);
    } catch (Exception e) {
        Game.reportException(e);
        throw new IOException();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) JSONTokener(org.json.JSONTokener) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) PushbackInputStream(java.io.PushbackInputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IOException(java.io.IOException) JSONException(org.json.JSONException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 50 with PushbackInputStream

use of java.io.PushbackInputStream in project nutz by nutzam.

the class Streams method utf8filte.

/**
 * 判断并移除UTF-8的BOM头
 */
public static InputStream utf8filte(InputStream in) {
    try {
        if (in.available() == -1)
            return in;
        PushbackInputStream pis = new PushbackInputStream(in, 3);
        byte[] header = new byte[3];
        int len = pis.read(header, 0, 3);
        if (len < 1)
            return in;
        if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) {
            pis.unread(header, 0, len);
        }
        return pis;
    } catch (IOException e) {
        throw Lang.wrapThrow(e);
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)130 IOException (java.io.IOException)61 InputStream (java.io.InputStream)34 ByteArrayInputStream (java.io.ByteArrayInputStream)21 FileInputStream (java.io.FileInputStream)10 GZIPInputStream (java.util.zip.GZIPInputStream)10 InputStreamReader (java.io.InputStreamReader)8 File (java.io.File)5 CertificateException (java.security.cert.CertificateException)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedReader (java.io.BufferedReader)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Charset (java.nio.charset.Charset)3