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);
}
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);
}
}
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;
}
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();
}
}
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);
}
}
Aggregations