/** * Checks the InputStream if it contains GZIP compressed data * * @param inputStream InputStream to be checked * @return true or false if the stream contains GZIP compressed data * @throws java.io.IOException if read from inputStream fails */ public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException { if (inputStream == null) return false; byte[] signature = new byte[2]; int count = 0; try { while (count < 2) { int readCount = inputStream.read(signature, count, 2 - count); if (readCount < 0) return false; count = count + readCount; } } finally { inputStream.unread(signature, 0, count); } int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00); return GZIPInputStream.GZIP_MAGIC == streamHeader; }
/** Tests whether InputStream contains serialized data * @param pbStream is pushback input stream; tests 4 bytes and then returns them back * @return true if the file has serialized form */ static private final boolean isSerialized(PushbackInputStream pbStream) throws IOException { int[] serialPattern = { '\u00AC', '\u00ED', '\u0000', '\u0005' }; //NOI18N patern for serialized objects byte[] checkedArray = new byte[serialPattern.length]; int unsignedConv = 0; pbStream.read(checkedArray, 0, checkedArray.length); pbStream.unread(checkedArray); for (int i = 0; i < checkedArray.length; i++) { unsignedConv = (checkedArray[i] < 0) ? (checkedArray[i] + 256) : checkedArray[i]; if (serialPattern[i] != unsignedConv) { return false; } } return true; }
/** * @param in the stream to wrap * @throws IOException */ public DeobfuscatingInputStream(InputStream in) throws IOException { super(null); final byte[] header = new byte[ObfuscatingOutputStream.HEADER.length()]; readFully(in, header, 0, header.length); if (new String(header, "UTF-8").equals(ObfuscatingOutputStream.HEADER)) { this.in = new DeobfuscatingInputStreamImpl(in); } else { final PushbackInputStream pin = new PushbackInputStream(in, header.length); pin.unread(header); this.in = pin; } }
/** * Fills {@code buf} with data from the given input stream and * returns an input stream from which you can still read all data, * including the data in buf. * * @param in The stream to read from. * @param buf The buffer to fill entirely with data. * @return A stream which holds all the data {@code in} did. * @throws EOFException on unexpected end-of-file. * @throws IOException on any I/O error. */ private static InputStream readAhead( final @WillNotClose InputStream in, final byte[] buf) throws EOFException, IOException { if (in.markSupported()) { in.mark(buf.length); new DataInputStream(in).readFully(buf); in.reset(); return in; } else { final PushbackInputStream pin = new PushbackInputStream(in, buf.length); new DataInputStream(pin).readFully(buf); pin.unread(buf); return pin; } }
/** * In uuencoded buffers, encoded lines start with a character that * represents the number of bytes encoded in this line. The last * line of input is always a line that starts with a single space * character, which would be a zero length line. */ protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException { int c; c = inStream.read(); if (c == ' ') { c = inStream.read(); /* discard the (first)trailing CR or LF */ c = inStream.read(); /* check for a second one */ if ((c != '\n') && (c != -1)) inStream.unread (c); throw new CEStreamExhausted(); } else if (c == -1) { throw new CEFormatException("UUDecoder: Short Buffer."); } c = (c - ' ') & 0x3f; if (c > bytesPerLine()) { throw new CEFormatException("UUDecoder: Bad Line Length."); } return (c); }
/** * Find the end of the line for the next operation. * The following sequences are recognized as end-of-line * CR, CR LF, or LF */ protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException { int c; while (true) { c = inStream.read(); if (c == -1) { throw new CEStreamExhausted(); } if (c == '\n') { break; } if (c == '\r') { c = inStream.read(); if ((c != '\n') && (c != -1)) { inStream.unread (c); } break; } } }
/** * Grants access to everyone.Removes authentication related bytes from the * stream, when a SOCKS5 connection is being made, selects an authentication * NONE. */ public ServerAuthenticator startSession(Socket s) throws IOException { final PushbackInputStream in = new PushbackInputStream(s .getInputStream()); final OutputStream out = s.getOutputStream(); final int version = in.read(); if (version == 5) { if (!selectSocks5Authentication(in, out, 0)) { return null; } } else if (version == 4) { // Else it is the request message already, version 4 in.unread(version); } else { return null; } return new ServerAuthenticatorNone(in, out); }
/** * Decode a UU atom. Note that if l is less than 3 we don't write * the extra bits, however the encoder always encodes 4 character * groups even when they are not needed. */ protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l) throws IOException { int i, c1, c2, c3, c4; int a, b, c; StringBuffer x = new StringBuffer(); for (i = 0; i < 4; i++) { c1 = inStream.read(); if (c1 == -1) { throw new CEStreamExhausted(); } x.append((char)c1); decoderBuffer[i] = (byte) ((c1 - ' ') & 0x3f); } a = ((decoderBuffer[0] << 2) & 0xfc) | ((decoderBuffer[1] >>> 4) & 3); b = ((decoderBuffer[1] << 4) & 0xf0) | ((decoderBuffer[2] >>> 2) & 0xf); c = ((decoderBuffer[2] << 6) & 0xc0) | (decoderBuffer[3] & 0x3f); outStream.write((byte)(a & 0xff)); if (l > 1) { outStream.write((byte)( b & 0xff)); } if (l > 2) { outStream.write((byte)(c&0xff)); } }
public static boolean isInputStreamGZIPCompressed(PushbackInputStream inputStream) throws IOException { boolean z = true; if (inputStream == null) { return false; } byte[] signature = new byte[2]; int readStatus = inputStream.read(signature); inputStream.unread(signature); int streamHeader = (signature[0] & 255) | ((signature[1] << 8) & MotionEventCompat .ACTION_POINTER_INDEX_MASK); if (!(readStatus == 2 && 35615 == streamHeader)) { z = false; } return z; }
@Override public InputStream getContent() throws IOException { wrappedStream = wrappedEntity.getContent(); pushbackStream = new PushbackInputStream(wrappedStream, 2); if (isInputStreamGZIPCompressed(pushbackStream)) { gzippedStream = new GZIPInputStream(pushbackStream); return gzippedStream; } else { return pushbackStream; } }
/** * Creates a new ZIP input stream. * * @param in the actual input stream */ public ZipInputStream(InputStream in) { super(new PushbackInputStream(in, 512), new Inflater(true), 512); usesDefaultInflater = true; if (in == null) { throw new NullPointerException("in is null"); } }
private void readEnd(ZipEntry e) throws IOException { int n = inf.getRemaining(); if (n > 0) { ((PushbackInputStream) in).unread(buf, len - n, n); } if ((flag & 8) == 8) { /* "Data Descriptor" present */ readFully(tmpbuf, 0, EXTHDR); long sig = get32(tmpbuf, 0); if (sig != EXTSIG) { // no EXTSIG present e.crc = sig; e.csize = get32(tmpbuf, EXTSIZ - EXTCRC); e.size = get32(tmpbuf, EXTLEN - EXTCRC); ((PushbackInputStream) in).unread(tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC); } else { e.crc = get32(tmpbuf, EXTCRC); e.csize = get32(tmpbuf, EXTSIZ); e.size = get32(tmpbuf, EXTLEN); } } if (e.size != inf.getBytesWritten()) { throw new ZipException("invalid entry size (expected " + e.size + " but got " + inf.getBytesWritten() + " bytes)"); } if (e.csize != inf.getBytesRead()) { throw new ZipException("invalid entry compressed size (expected " + e.csize + " but got " + inf.getBytesRead() + " bytes)"); } if (e.crc != crc.getValue()) { throw new ZipException("invalid entry CRC (expected 0x" + Long.toHexString(e.crc) + " but got 0x" + Long.toHexString(crc.getValue()) + ")"); } }
public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { this.tempFileManager = tempFileManager; this.inputStream = new PushbackInputStream(inputStream, BUFSIZE); this.outputStream = outputStream; String remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString(); headers = new HashMap<String, String>(); headers.put("remote-addr", remoteIp); headers.put("http-client-ip", remoteIp); }
/** * Decode the text from the InputStream and write the decoded * octets to the OutputStream. This method runs until the stream * is exhausted. * @exception CEFormatException An error has occurred while decoding * @exception CEStreamExhausted The input stream is unexpectedly out of data */ public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException { int i; int totalBytes = 0; PushbackInputStream ps = new PushbackInputStream (aStream); decodeBufferPrefix(ps, bStream); while (true) { int length; try { length = decodeLinePrefix(ps, bStream); for (i = 0; (i+bytesPerAtom()) < length; i += bytesPerAtom()) { decodeAtom(ps, bStream, bytesPerAtom()); totalBytes += bytesPerAtom(); } if ((i + bytesPerAtom()) == length) { decodeAtom(ps, bStream, bytesPerAtom()); totalBytes += bytesPerAtom(); } else { decodeAtom(ps, bStream, length - i); totalBytes += (length - i); } decodeLineSuffix(ps, bStream); } catch (CEStreamExhausted e) { break; } } decodeBufferSuffix(ps, bStream); }
public PushbackInputStream getInputStream() throws IOException { if (input == null) { throw new IOException( "login session isn't opened or is already closed."); } return input; }
private void decodeBuffer(InputStream paramInputStream, OutputStream paramOutputStream) throws IOException { PushbackInputStream localPushbackInputStream = new PushbackInputStream(paramInputStream); int j = 0; while (true) { try { int k = bytesPerLine(); int i = 0; if (i + bytesPerAtom() < k) { decodeAtom(localPushbackInputStream, paramOutputStream, bytesPerAtom()); j += bytesPerAtom(); i += bytesPerAtom(); continue; } if (i + bytesPerAtom() == k) { decodeAtom(localPushbackInputStream, paramOutputStream, bytesPerAtom()); j += bytesPerAtom(); } else { decodeAtom(localPushbackInputStream, paramOutputStream, k - i); j += k - i; } } catch (RuntimeException e) { String.valueOf(j); break; } } }
/** * Checks the InputStream if it contains GZIP compressed data * * @param inputStream InputStream to be checked * @return true or false if the stream contains GZIP compressed data * @throws java.io.IOException */ public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException { if (inputStream == null) return false; byte[] signature = new byte[2]; int readStatus = inputStream.read(signature); inputStream.unread(signature); int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00); return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader; }
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext parseContext) throws IOException, SAXException, TikaException { byte[] initial4 = new byte[4]; InputStream wrapped; // Preserve TikaInputStreams as TikaInputStreams as they require less memory to process if (stream.markSupported()) { stream.mark(initial4.length); IOUtils.readFully(stream, initial4); stream.reset(); wrapped = stream; } else { PushbackInputStream inp = new PushbackInputStream(stream, 4); IOUtils.readFully(inp, initial4); inp.unread(initial4); wrapped = inp; } // Which is it? if(initial4[0] == POIFSConstants.OOXML_FILE_HEADER[0] && initial4[1] == POIFSConstants.OOXML_FILE_HEADER[1] && initial4[2] == POIFSConstants.OOXML_FILE_HEADER[2] && initial4[3] == POIFSConstants.OOXML_FILE_HEADER[3]) { ooxmlParser.parse(wrapped, handler, metadata, parseContext); } else { ole2Parser.parse(wrapped, handler, metadata, parseContext); } }
/** * 145 * Decode the text from the InputStream and write the decoded 146 * * octets to the OutputStream. This method runs until the stream 147 * is * exhausted. 148 * @exception CEFormatException An error has occured while * decoding 149 * @exception CEStreamExhausted The input stream is * unexpectedly out of data 150 */ public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException { int i; @SuppressWarnings("unused") int totalBytes = 0; PushbackInputStream ps = new PushbackInputStream(aStream); decodeBufferPrefix(ps, bStream); while (true) { int length; try { length = decodeLinePrefix(ps, bStream); for (i = 0; (i + bytesPerAtom()) < length; i += bytesPerAtom()) { decodeAtom(ps, bStream, bytesPerAtom()); totalBytes += bytesPerAtom(); } if ((i + bytesPerAtom()) == length) { decodeAtom(ps, bStream, bytesPerAtom()); totalBytes += bytesPerAtom(); } else { decodeAtom(ps, bStream, length - i); totalBytes += (length - i); } decodeLineSuffix(ps, bStream); } catch (RuntimeException e) { break; } } decodeBufferSuffix(ps, bStream); }
public static String readLine(PushbackInputStream in) throws IOException { char buf[] = new char[128]; int room = buf.length; int offset = 0; int c; loop: while (true) { switch (c = in.read()) { case -1: case '\n': break loop; case '\r': int c2 = in.read(); if ((c2 != '\n') && (c2 != -1)) in.unread(c2); break loop; default: if (--room < 0) { char[] lineBuffer = buf; buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy((Object)lineBuffer, 0, (Object)buf, 0, offset); } buf[offset++] = (char) c; break; } } if ((c == -1) && (offset == 0)) return null; return String.copyValueOf(buf, 0, offset); }
/** * Constructs an XmlHeaderAwareReader. * * @param in the {@link InputStream} * @throws UnsupportedEncodingException if the encoding is not supported * @throws IOException occurred while reading the XML header * @since 1.3 */ public XmlHeaderAwareReader(final InputStream in) throws UnsupportedEncodingException, IOException { final PushbackInputStream[] pin = new PushbackInputStream[]{in instanceof PushbackInputStream ? (PushbackInputStream)in : new PushbackInputStream(in, 64)}; final Map<String, String> header = getHeader(pin); version = Double.parseDouble(header.get(KEY_VERSION)); reader = new InputStreamReader(pin[0], header.get(KEY_ENCODING)); }
/** * Decode the text from the InputStream and write the decoded * octets to the OutputStream. This method runs until the stream * is exhausted. * @exception CEFormatException An error has occured while decoding * @exception CEStreamExhausted The input stream is unexpectedly out of data */ public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException { int i; int totalBytes = 0; PushbackInputStream ps = new PushbackInputStream (aStream); decodeBufferPrefix(ps, bStream); while (true) { int length; try { length = decodeLinePrefix(ps, bStream); for (i = 0; (i+bytesPerAtom()) < length; i += bytesPerAtom()) { decodeAtom(ps, bStream, bytesPerAtom()); totalBytes += bytesPerAtom(); } if ((i + bytesPerAtom()) == length) { decodeAtom(ps, bStream, bytesPerAtom()); totalBytes += bytesPerAtom(); } else { decodeAtom(ps, bStream, length - i); totalBytes += (length - i); } decodeLineSuffix(ps, bStream); } catch (CEStreamExhausted e) { break; } } decodeBufferSuffix(ps, bStream); }
/** * decodeLinePrefix reads the sequence number and the number of * encoded bytes from the line. If the sequence number is not the * previous sequence number + 1 then an exception is thrown. * UCE lines are line terminator immune, they all start with * * so the other thing this method does is scan for the next line * by looking for the * character. * * @exception CEFormatException out of sequence lines detected. */ protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException { int i; int nLen, nSeq; byte xtmp[]; int c; crc.value = 0; while (true) { c = inStream.read(tmp, 0, 1); if (c == -1) { throw new CEStreamExhausted(); } if (tmp[0] == '*') { break; } } lineAndSeq.reset(); decodeAtom(inStream, lineAndSeq, 2); xtmp = lineAndSeq.toByteArray(); nLen = xtmp[0] & 0xff; nSeq = xtmp[1] & 0xff; if (nSeq != sequence) { throw new CEFormatException("UCDecoder: Out of sequence line."); } sequence = (sequence + 1) & 0xff; return (nLen); }
/** * this method reads the CRC that is at the end of every line and * verifies that it matches the computed CRC. * * @exception CEFormatException if CRC check fails. */ protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException { int i; int lineCRC = crc.value; int readCRC; byte tmp[]; lineAndSeq.reset(); decodeAtom(inStream, lineAndSeq, 2); tmp = lineAndSeq.toByteArray(); readCRC = ((tmp[0] << 8) & 0xFF00) + (tmp[1] & 0xff); if (readCRC != lineCRC) { throw new CEFormatException("UCDecoder: CRC check failed."); } }
/** * Checks the InputStream if it contains GZIP compressed data * * @param inputStream InputStream to be checked * @return true or false if the stream contains GZIP compressed data * @throws IOException */ public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException { if (inputStream == null) return false; byte[] signature = new byte[2]; int readStatus = inputStream.read(signature); inputStream.unread(signature); int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00); return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader; }
/** * UUencoded files have a buffer suffix which consists of the word * end. This line should immediately follow the line with a single * space in it. */ protected void decodeBufferSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException { int c; c = inStream.read(decoderBuffer); if ((decoderBuffer[0] != 'e') || (decoderBuffer[1] != 'n') || (decoderBuffer[2] != 'd')) { throw new CEFormatException("UUDecoder: Missing 'end' line."); } }
static private String detectEncoding(PushbackInputStream in) throws IOException { String encoding = UTF8; int b1 = in.read(); if (b1 != -1) { int b2 = in.read(); if (b2 != -1) { in.unread(b2); if ((b1 == 0xFF && b2 == 0xFE) || (b1 == 0xFE && b2 == 0xFF)) encoding = UTF16; } in.unread(b1); } return encoding; }
/** * 装饰一下,返回可以重读的InputStream * * @return dummy * @throws IOException */ @Override public InputStream getBody() throws IOException { InputStream body = this.delegate.getBody(); if (body == null || body.markSupported() || body instanceof PushbackInputStream) { return body; } else if (this.pushbackInputStream == null) { this.pushbackInputStream = new PushbackInputStream(body, WX_API_ERROR_CODE_END); } return this.pushbackInputStream; }
public InputStream getContent() throws IOException { this.wrappedStream = this.wrappedEntity.getContent(); this.pushbackStream = new PushbackInputStream(this.wrappedStream, 2); if (!AsyncHttpClient.isInputStreamGZIPCompressed(this.pushbackStream)) { return this.pushbackStream; } this.gzippedStream = new GZIPInputStream(this.pushbackStream); return this.gzippedStream; }