Java Code Examples for java.io.PushbackInputStream#unread()
The following examples show how to use
java.io.PushbackInputStream#unread() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PostVersionedIOReadableWritable.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
/** * This read attempts to first identify if the input view contains the special * {@link #VERSIONED_IDENTIFIER} by reading and buffering the first few bytes. * If identified to be versioned, the usual version resolution read path * in {@link VersionedIOReadableWritable#read(DataInputView)} is invoked. * Otherwise, we "reset" the input stream by pushing back the read buffered bytes * into the stream. */ public final void read(InputStream inputStream) throws IOException { byte[] tmp = new byte[VERSIONED_IDENTIFIER.length]; inputStream.read(tmp); if (Arrays.equals(tmp, VERSIONED_IDENTIFIER)) { DataInputView inputView = new DataInputViewStreamWrapper(inputStream); super.read(inputView); read(inputView, true); } else { PushbackInputStream resetStream = new PushbackInputStream(inputStream, VERSIONED_IDENTIFIER.length); resetStream.unread(tmp); read(new DataInputViewStreamWrapper(resetStream), false); } }
Example 2
Source File: UUDecoder.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * 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; } } }
Example 3
Source File: ChunkedUtil.java From cxf with Apache License 2.0 | 6 votes |
/** * @param connection the given HttpURLConnection * @return an input stream containing the response content if non-empty */ private static InputStream getNonEmptyContent( HttpURLConnection connection ) { InputStream in = null; try { PushbackInputStream pin = new PushbackInputStream(connection.getInputStream()); int c = pin.read(); if (c != -1) { pin.unread((byte)c); in = pin; } } catch (IOException ioe) { // ignore } return in; }
Example 4
Source File: UUDecoder.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * 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; } } }
Example 5
Source File: IOUtils.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Peeks at the first N bytes of the stream. Returns those bytes, but * with the stream unaffected. Requires a stream that supports mark/reset, * or a PushbackInputStream. If the stream has >0 but <N bytes, * remaining bytes will be zero. * @throws EmptyFileException if the stream is empty */ public static byte[] peekFirstNBytes(InputStream stream, int limit) throws IOException, EmptyFileException { stream.mark(limit); ByteArrayOutputStream bos = new ByteArrayOutputStream(limit); copy(new BoundedInputStream(stream, limit), bos); int readBytes = bos.size(); if (readBytes == 0) { throw new EmptyFileException(); } if (readBytes < limit) { bos.write(new byte[limit-readBytes]); } byte peekedBytes[] = bos.toByteArray(); if(stream instanceof PushbackInputStream) { PushbackInputStream pin = (PushbackInputStream)stream; pin.unread(peekedBytes, 0, readBytes); } else { stream.reset(); } return peekedBytes; }
Example 6
Source File: AttachmentDeserializer.java From cxf with Apache License 2.0 | 6 votes |
private String findBoundaryFromInputStream() throws IOException { InputStream is = message.getContent(InputStream.class); //boundary should definitely be in the first 2K; PushbackInputStream in = new PushbackInputStream(is, 4096); byte[] buf = new byte[2048]; int i = in.read(buf); int len = i; while (i > 0 && len < buf.length) { i = in.read(buf, len, buf.length - len); if (i > 0) { len += i; } } String msg = IOUtils.newStringFromBytes(buf, 0, len); in.unread(buf, 0, len); // Reset the input stream since we'll need it again later message.setContent(InputStream.class, in); // Use regex to get the boundary and return null if it's not found Matcher m = INPUT_STREAM_BOUNDARY_PATTERN.matcher(msg); return m.find() ? "--" + m.group(1) : null; }
Example 7
Source File: UUDecoder.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * 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); }
Example 8
Source File: ServerAuthenticatorNone.java From j2ssh-maverick with GNU Lesser General Public License v3.0 | 6 votes |
/** 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{ PushbackInputStream in = new PushbackInputStream(s.getInputStream()); OutputStream out = s.getOutputStream(); int version = in.read(); if(version == 5){ if(!selectSocks5Authentication(in,out,0)) return null; }else if(version == 4){ //Else it is the request message allready, version 4 in.unread(version); }else return null; return new ServerAuthenticatorNone(in,out); }
Example 9
Source File: AbstractMessageConverterMethodArgumentResolver.java From spring-analysis-note with MIT License | 6 votes |
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException { this.headers = inputMessage.getHeaders(); InputStream inputStream = inputMessage.getBody(); if (inputStream.markSupported()) { inputStream.mark(1); this.body = (inputStream.read() != -1 ? inputStream : null); inputStream.reset(); } else { PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream); int b = pushbackInputStream.read(); if (b == -1) { this.body = null; } else { this.body = pushbackInputStream; pushbackInputStream.unread(b); } } }
Example 10
Source File: InflaterInputStream.java From zip4j with Apache License 2.0 | 5 votes |
@Override public void pushBackInputStreamIfNecessary(PushbackInputStream pushbackInputStream) throws IOException { int n = inflater.getRemaining(); if (n > 0) { byte[] rawDataCache = getLastReadRawDataCache(); pushbackInputStream.unread(rawDataCache, len - n, n); } }
Example 11
Source File: UnicodeReader.java From sailfish-core with Apache License 2.0 | 5 votes |
/** * Construct UnicodeReader * @param in Input stream. * @param defaultEncoding Default encoding to be used if BOM is not found, * or <code>null</code> to use system default encoding. * @throws IOException If an I/O error occurs. */ public UnicodeReader(InputStream in, String defaultEncoding) throws IOException { byte[] bom = new byte[BOM_SIZE]; String encoding; int unread; PushbackInputStream pushbackStream = new PushbackInputStream(in, BOM_SIZE); int n = pushbackStream.read(bom, 0, bom.length); // Read ahead four bytes and check for BOM marks. if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) { encoding = "UTF-8"; unread = n - 3; } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { encoding = "UTF-16BE"; unread = n - 2; } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { encoding = "UTF-16LE"; unread = n - 2; } else if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) { encoding = "UTF-32BE"; unread = n - 4; } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) { encoding = "UTF-32LE"; unread = n - 4; } else { encoding = defaultEncoding; unread = n; } // Unread bytes if necessary and skip BOM marks. if (unread > 0) { pushbackStream.unread(bom, n - unread, unread); } else if (unread < -1) { pushbackStream.unread(bom, 0, 0); } // Use given encoding. reader = encoding == null ? new InputStreamReader(pushbackStream) : new InputStreamReader(pushbackStream, encoding); }
Example 12
Source File: RExpParser.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
static private InputStream init(PushbackInputStream is) throws IOException { byte[] gzipMagic = new byte[2]; ByteStreams.readFully(is, gzipMagic); is.unread(gzipMagic); if(Arrays.equals(RExpParser.GZIP_MAGIC, gzipMagic)){ return new GZIPInputStream(is); } return is; }
Example 13
Source File: AsyncHttpClient.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * 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; }
Example 14
Source File: HttpResponse.java From nutch-selenium with Apache License 2.0 | 5 votes |
private void parseHeaders(PushbackInputStream in, StringBuffer line) throws IOException, HttpException { while (readLine(in, line, true) != 0) { // handle HTTP responses with missing blank line after headers int pos; if (((pos = line.indexOf("<!DOCTYPE")) != -1) || ((pos = line.indexOf("<HTML")) != -1) || ((pos = line.indexOf("<html")) != -1)) { in.unread(line.substring(pos).getBytes("UTF-8")); line.setLength(pos); try { //TODO: (CM) We don't know the header names here //since we're just handling them generically. It would //be nice to provide some sort of mapping function here //for the returned header names to the standard metadata //names in the ParseData class processHeaderLine(line); } catch (Exception e) { // fixme: Http.LOG.warn("Error: ", e); } return; } processHeaderLine(line); } }
Example 15
Source File: NBTUtil.java From NBT with MIT License | 5 votes |
private static InputStream detectDecompression(InputStream is) throws IOException { PushbackInputStream pbis = new PushbackInputStream(is, 2); int signature = (pbis.read() & 0xFF) + (pbis.read() << 8); pbis.unread(signature >> 8); pbis.unread(signature & 0xFF); if (signature == GZIPInputStream.GZIP_MAGIC) { return new GZIPInputStream(pbis); } return pbis; }
Example 16
Source File: MaildirMessage.java From james-project with Apache License 2.0 | 5 votes |
/** * Return the position in the given {@link InputStream} at which the Body of * the MailboxMessage starts */ private int bodyStartOctet(InputStream msgIn) throws IOException { // we need to pushback maximal 3 bytes PushbackInputStream in = new PushbackInputStream(msgIn, 3); int localBodyStartOctet = in.available(); int i; int count = 0; while ((i = in.read()) != -1 && in.available() > 4) { if (i == 0x0D) { int a = in.read(); if (a == 0x0A) { int b = in.read(); if (b == 0x0D) { int c = in.read(); if (c == 0x0A) { localBodyStartOctet = count + 4; break; } in.unread(c); } in.unread(b); } in.unread(a); } count++; } return localBodyStartOctet; }
Example 17
Source File: CompactParseable.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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; }
Example 18
Source File: UUDecoder.java From javaide with GNU General Public License v3.0 | 4 votes |
/** * For uuencoded buffers, the data begins with a line of the form: * begin MODE FILENAME * This line always starts in column 1. */ protected void decodeBufferPrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException { int c; StringBuffer q = new StringBuffer(32); String r; boolean sawNewLine; /* * This works by ripping through the buffer until it finds a 'begin' * line or the end of the buffer. */ sawNewLine = true; while (true) { c = inStream.read(); if (c == -1) { throw new CEFormatException("UUDecoder: No begin line."); } if ((c == 'b') && sawNewLine){ c = inStream.read(); if (c == 'e') { break; } } sawNewLine = (c == '\n') || (c == '\r'); } /* * Now we think its begin, (we've seen ^be) so verify it here. */ while ((c != '\n') && (c != '\r')) { c = inStream.read(); if (c == -1) { throw new CEFormatException("UUDecoder: No begin line."); } if ((c != '\n') && (c != '\r')) { q.append((char)c); } } r = q.toString(); if (r.indexOf(' ') != 3) { throw new CEFormatException("UUDecoder: Malformed begin line."); } mode = Integer.parseInt(r.substring(4,7)); bufferName = r.substring(r.indexOf(' ',6)+1); /* * Check for \n after \r */ if (c == '\r') { c = inStream.read (); if ((c != '\n') && (c != -1)) inStream.unread (c); } }
Example 19
Source File: EmptyBodyFilter.java From camunda-bpm-platform with Apache License 2.0 | 4 votes |
@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); } }
Example 20
Source File: RpcUnicodeInputStream.java From p4ic4idea with Apache License 2.0 | 4 votes |
/** * Constructs a new RpcUnicodeInputStream that wraps the InputStream. */ public RpcUnicodeInputStream(final InputStream inputStream) throws NullPointerException, IOException { if (inputStream == null) { throw new NullPointerException("Null inputstream passed to the RpcUnicodeInputStream constructor."); } in = new PushbackInputStream(inputStream, 4); final byte bom[] = new byte[4]; final int read = in.read(bom); switch (read) { case 4: if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) { this.bom = BOM.UTF_32_LE; break; } else if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) { this.bom = BOM.UTF_32_BE; break; } case 3: if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) { this.bom = BOM.UTF_8; break; } case 2: if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { this.bom = BOM.UTF_16_LE; break; } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { this.bom = BOM.UTF_16_BE; break; } default: this.bom = BOM.NONE; break; } if (read > 0) in.unread(bom, 0, read); }