/** * Method to verify if receieved stream has content or its empty * * @param stream Stream to check * @return verified stream * @throws IOException */ private InputStream verifyContentReceieved(InputStream stream) throws IOException { if (stream == null) { // Check if its null logger.debug("Request stream received as null"); return null; } else if (stream.markSupported()) { // Check stream supports mark/reset // mark() and read the first byte just to check stream.mark(1); final int bytesRead = stream.read(new byte[1]); if (bytesRead != -1) { // stream not empty stream.reset(); // reset the stream as if untouched return stream; } else { // stream empty logger.debug("Request received with empty body"); return null; } } else { // Panic! this stream does not support mark/reset, try with PushbackInputStream as a last // resort int bytesRead; PushbackInputStream pbs = new PushbackInputStream(stream); if ((bytesRead = pbs.read()) != -1) { // Contents detected, unread and return pbs.unread(bytesRead); return pbs; } else { // Empty stream detected logger.debug("Request received with empty body!"); return null; } } }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @ApiOperation( value = "Returns a GEO label LML representation", notes = "Requires metadata/feedback documents as data stream") @ApiResponses({@ApiResponse(code = 400, message = "Error in feedback/metadata document")}) public Label getLabelByStream( /* @ApiParam("Metadata document") */ @FormDataParam(Constants.PARAM_METADATA) InputStream metadataInputStream, /* @ApiParam("Feedback document") */ @FormDataParam(Constants.PARAM_FEEDBACK) InputStream feedbackInputStream) throws IOException { MetadataTransformer metadataTransformer = this.transformer.get(); Label label = new Label(); label.setMetadataUrl(new URL("http://not.available.for.direct.upload")); label.setFeedbackUrl(new URL("http://not.available.for.direct.upload")); PushbackInputStream tempStream; boolean hasData = false; if (metadataInputStream != null) { tempStream = new PushbackInputStream(metadataInputStream); int t = tempStream.read(); if (t != -1) { tempStream.unread(t); log.debug("Reading from metadata stream..."); metadataTransformer.updateGeoLabel(tempStream, label); hasData = true; } else tempStream.close(); } if (feedbackInputStream != null) { tempStream = new PushbackInputStream(feedbackInputStream); int t = tempStream.read(); if (t != -1) { tempStream.unread(t); log.debug("Reading from feedback stream..."); metadataTransformer.updateGeoLabel(tempStream, label); hasData = true; } else tempStream.close(); } if (!hasData) throw new WebApplicationException( Response.status(Response.Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("No metadata or feedback file specified") .build()); return label; }
/** Determines if the next 4 bytes of the stream represent a frame header. */ public boolean isSyncCurrentPosition(int syncmode) throws BitstreamException { int read = readBytes(syncbuf, 0, 4); int headerstring = ((syncbuf[0] << 24) & 0xFF000000) | ((syncbuf[1] << 16) & 0x00FF0000) | ((syncbuf[2] << 8) & 0x0000FF00) | ((syncbuf[3] << 0) & 0x000000FF); try { source.unread(syncbuf, 0, read); } catch (IOException ex) { } boolean sync = false; switch (read) { case 0: sync = true; break; case 4: sync = isSyncMark(headerstring, syncmode, syncword); break; } return sync; }
public static String unGzipBytesToString(InputStream in) { try { PushbackInputStream pis = new PushbackInputStream(in, 2); byte[] signature = new byte[2]; pis.read(signature); pis.unread(signature); int head = ((signature[0] & 0x00FF) | ((signature[1] << 8) & 0xFF00)); if (head != GZIPInputStream.GZIP_MAGIC) { return new String(toByteArray(pis), "UTF-8").trim(); } GZIPInputStream gzip = new GZIPInputStream(pis); byte[] readBuf = new byte[8 * 1024]; ByteArrayOutputStream outputByte = new ByteArrayOutputStream(); int readCount = 0; do { readCount = gzip.read(readBuf); if (readCount > 0) { outputByte.write(readBuf, 0, readCount); } } while (readCount > 0); closeQuietly(gzip); closeQuietly(pis); closeQuietly(in); if (outputByte.size() > 0) { return new String(outputByte.toByteArray(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } return null; }
/** * This method allows the caller to retrieve any data that might have been sent despite the fact * that an error occurred. For example, the HTML page sent along with a 404 File Not Found error. * If the socket is not connected, or if no error occurred or no data was returned, this method * returns <code>null</code>. * * @return An <code>InputStream</code> for reading error data. */ public InputStream getErrorStream() { if (!connected) return (null); int code; try { code = getResponseCode(); } catch (IOException e) { code = -1; } if (code == -1) return (null); if (((code / 100) != 4) || ((code / 100) != 5)) return (null); try { PushbackInputStream pbis = new PushbackInputStream(getInputStream()); int i = pbis.read(); if (i == -1) return (null); pbis.unread(i); return (pbis); } catch (IOException e) { return (null); } }
/** * Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only * BOM bytes are skipped. */ protected void init() throws IOException { if (internalIn2 != null) return; Charset encoding; byte bom[] = new byte[BOM_SIZE]; int n, unread; n = internalIn.read(bom, 0, bom.length); if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) { encoding = UTF8; unread = n - 3; } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { encoding = UTF16BE; unread = n - 2; } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { encoding = UTF16LE; unread = n - 2; } else { // Unicode BOM mark not found, unread all bytes encoding = UTF8; unread = n; } if (unread > 0) internalIn.unread(bom, (n - unread), unread); // Use given encoding internalIn2 = new InputStreamReader(internalIn, encoding); }
public boolean hasNextChunk() throws Exception { int b = in.read(); if (b < 0) { return false; } else { in.unread(b); return true; } }
private void ungetChar(int c) throws IOException { if (c == -1) { return; } is.unread(c); if (c == '\n') { line--; } }
public static ImageType guessImageType(PushbackInputStream is) throws IOException { // Read the first ImageIO.IMAGE_MAGIC_NUMBER_LEN bytes byte[] magicNumber = new byte[IMAGE_MAGIC_NUMBER_LEN]; is.read(magicNumber); ImageType imageType = guessImageType(magicNumber); is.unread(magicNumber); // reset stream pointer return imageType; }
// REVIEW: add new error codes for this. public void unreadFrame() throws BitstreamException { if (wordpointer == -1 && bitindex == -1 && (framesize > 0)) { try { source.unread(frame_bytes, 0, framesize); } catch (IOException ex) { throw newBitstreamException(STREAM_ERROR); } } }
/** * Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only * BOM bytes are skipped. */ private void init(InputStream in, String defaultEnc) throws IOException { String encoding; byte bom[] = new byte[BOM_MAX_SIZE]; int n, unread; PushbackInputStream internalIn = new PushbackInputStream(in, BOM_MAX_SIZE); n = internalIn.read(bom, 0, bom.length); 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 { // Unicode BOM mark not found, unread all bytes encoding = defaultEnc; unread = n; } if (unread > 0) internalIn.unread(bom, (n - unread), unread); else if (unread < -1) internalIn.unread(bom, 0, 0); // Use BOM or default encoding if (encoding == null) { delegate = new InputStreamReader(internalIn); } else { delegate = new InputStreamReader(internalIn, encoding); } }
private int getChar() throws IOException { int c = is.read(); if (c == '\r') { int next = is.read(); if (next != '\n') is.unread(next); c = '\n'; } if (c == '\n') line++; return c; }
private int normalizeRead(PushbackInputStream str) throws IOException { int ret = str.read(); if (ret == '\r') { int tmp = str.read(); if (tmp != '\n') { str.unread(tmp); } ret = '\n'; } return ret; }
public static InputStream decompressStream(InputStream input) throws IOException { PushbackInputStream pb = new PushbackInputStream(input, 2); // we need a pushbackstream to look ahead byte[] signature = new byte[2]; pb.read(signature); // read the signature pb.unread(signature); // push back the signature to the stream if (signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b) // check if matches standard gzip magic number return new GZIPInputStream(pb); else return pb; }
public Object read(InputStream is) throws IOException { PushbackInputStream pbis = (PushbackInputStream) pbMap.get(is); if (null == pbis) { pbis = new PushbackInputStream(is, PUSHBACK_BUFFER_SIZE); PushbackInputStream prev = (PushbackInputStream) pbMap.putIfAbsent(is, pbis); pbis = null == prev ? pbis : prev; } int len = -1; try { // read until xml pattern is seen (and then pushed back) or no more data // to read. return all data as message byte[] buffer = new byte[READ_BUFFER_SIZE]; StringBuffer message = new StringBuffer(READ_BUFFER_SIZE); int patternIndex = -1; boolean repeat; do { len = safeRead(pbis, buffer); if (len >= 0) { // TODO take encoding into account, ideally from the incoming XML message.append(new String(buffer, 0, len)); // start search at 2nd character in buffer (index=1) to // indicate whether we have reached a new document. patternIndex = message.toString().indexOf(XML_PATTERN, 1); repeat = isRepeat(patternIndex, len, pbis.available()); } else { // never repeat on closed stream (and avoid calling available) repeat = false; } } while (repeat); if (patternIndex > 0) { // push back the start of the next message and // ignore the pushed-back characters in the return buffer pbis.unread(message.substring(patternIndex, message.length()).getBytes()); message.setLength(patternIndex); } // TODO encoding here, too... return nullEmptyArray(message.toString().getBytes()); } finally { // TODO - this doesn't seem very reliable, since loop above can end // without EOF. On the other hand, what else can we do? Entire logic // is not very dependable, IMHO. XmlMessageEOFProtocol is more likely // to be correct here, I think. // clear from map if stream has ended if (len < 0) { pbMap.remove(is); } } }
public InStream unread(int n) { try { // don't take the hit until we know we need to wrap // the raw input stream with a pushback stream if (!(in instanceof PushbackInputStream)) in = new PushbackInputStream(in, 128); ((PushbackInputStream) in).unread(n); return this; } catch (IOException e) { throw IOErr.make(e); } }
private static String getContentEncoding(PushbackInputStream is) throws IOException { byte[] signature = new byte[2]; int count = is.read(signature); is.unread(signature, 0, count); if ((count == signature.length) && Arrays.equals(Deployer.GZIP_SIGNATURE, signature)) { return "gzip"; } return null; }
/** * Converts a stream to a reader while detecting the encoding. * * @param stream the input for the XML data. * @param charsRead buffer where to put characters that have been read * @throws java.io.IOException if an I/O error occurred */ protected Reader stream2reader(InputStream stream, StringBuffer charsRead) throws IOException { PushbackInputStream pbstream = new PushbackInputStream(stream); int b = pbstream.read(); switch (b) { case 0x00: case 0xFE: case 0xFF: pbstream.unread(b); return new InputStreamReader(pbstream, "UTF-16"); case 0xEF: for (int i = 0; i < 2; i++) { pbstream.read(); } return new InputStreamReader(pbstream, "UTF-8"); case 0x3C: b = pbstream.read(); charsRead.append('<'); while ((b > 0) && (b != 0x3E)) { charsRead.append((char) b); b = pbstream.read(); } if (b > 0) { charsRead.append((char) b); } String encoding = this.getEncoding(charsRead.toString()); if (encoding == null) { return new InputStreamReader(pbstream, "UTF-8"); } charsRead.setLength(0); try { return new InputStreamReader(pbstream, encoding); } catch (UnsupportedEncodingException e) { return new InputStreamReader(pbstream, "UTF-8"); } default: charsRead.append((char) b); return new InputStreamReader(pbstream, "UTF-8"); } }
/** 判断并移除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); } }
private InputStream decompress(InputStream in) throws IOException { PushbackInputStream pushbackIn = new PushbackInputStream(in, 2); int b1 = pushbackIn.read(); int b2 = pushbackIn.read(); pushbackIn.unread(b2); pushbackIn.unread(b1); if (b1 == GzFileConnection.GZIP_MAGIC_BYTE1 && b2 == GzFileConnection.GZIP_MAGIC_BYTE2) { return new GZIPInputStream(pushbackIn); } else if (b1 == 0xFD && b2 == '7') { return new XZInputStream(pushbackIn); } return in; }
/** * Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only * BOM bytes are skipped. */ protected void init() throws IOException { if (internalIn2 != null) return; String encoding; byte bom[] = new byte[BOM_SIZE]; int n, unread; n = internalIn.read(bom, 0, bom.length); 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 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 { // Unicode BOM mark not found, unread all bytes encoding = defaultEnc; unread = n; } // System.out.println("read=" + n + ", unread=" + unread); if (unread > 0) internalIn.unread(bom, (n - unread), unread); // Use given encoding if (encoding == null) { internalIn2 = new InputStreamReader(internalIn); } else { internalIn2 = new InputStreamReader(internalIn, encoding); } }
@Override public void poll() { try { if (sendStream) { PushbackInputStream in = new PushbackInputStream(inputStream); // Block until we have some data int i = in.read(); // Roll back our read in.unread(i); MuleMessage message = new DefaultMuleMessage(connector.getMessageAdapter(in), connector.getMuleContext()); routeMessage(message, endpoint.isSynchronous()); } else { byte[] inputBuffer = new byte[bufferSize]; int len = inputStream.read(inputBuffer); if (len == -1) { return; } StringBuffer fullBuffer = new StringBuffer(bufferSize); while (len > 0) { fullBuffer.append(new String(inputBuffer, 0, len)); len = 0; // mark as read if (inputStream.available() > 0) { len = inputStream.read(inputBuffer); } } // Each line is a separate message String[] lines = fullBuffer.toString().split(SystemUtils.LINE_SEPARATOR); for (int i = 0; i < lines.length; ++i) { routeMessage( new DefaultMuleMessage( connector.getMessageAdapter(lines[i]), connector.getMuleContext()), endpoint.isSynchronous()); } } doConnect(); } catch (Exception e) { handleException(e); } }
/** * Reads from the compressed stream and stores the resulting uncompressed data into the byte * array. * * @return number of bytes read, or -1 upon EOF */ public int read(byte[] b, int off, int len) throws IOException { if (len <= 0 || off < 0 || off + len > b.length) return 0; if (_eof) return -1; // Read from uncompressed stream if (!_isGzip) return _in.read(b, off, len); try { int sublen; int length = 0; while (length < len) { if (_inflater.needsInput()) { _readBufferSize = _in.read(_readBuffer, 0, _readBuffer.length); if (_readBufferSize < 0) break; _inflater.setInput(_readBuffer, 0, _readBufferSize); } sublen = _inflater.inflate(b, off + length, len - length); _crc.update(b, off + length, sublen); _inputSize += sublen; _totalInputSize += sublen; length += sublen; // Unread gzip trailer and possibly beginning of appended gzip data. if (_inflater.finished()) { int remaining = _inflater.getRemaining(); _in.unread(_readBuffer, _readBufferSize - remaining, remaining); readTrailer(); int secondPart = read(b, off + length, len - length); return secondPart > 0 ? length + secondPart : length; } } return length; } catch (DataFormatException e) { throw new IOException(e.getMessage()); } }
/** * Generates a certificate revocation list (CRL) object and initializes it with the data read from * the input stream inStream. */ public CRL engineGenerateCRL(InputStream inStream) throws CRLException { if (currentCrlStream == null) { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } else if (currentCrlStream != inStream) // reset if input stream has changed { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } try { if (sCrlData != null) { if (sCrlDataObjectCount != sCrlData.size()) { return getCRL(); } else { sCrlData = null; sCrlDataObjectCount = 0; return null; } } PushbackInputStream pis = new PushbackInputStream(inStream); int tag = pis.read(); if (tag == -1) { return null; } pis.unread(tag); if (tag != 0x30) // assume ascii PEM encoded. { return readPEMCRL(pis); } else { // lazy evaluate to help processing of large CRLs return readDERCRL(new ASN1InputStream(pis, true)); } } catch (CRLException e) { throw e; } catch (Exception e) { throw new CRLException(e.toString()); } }
/** * Checks that the supplied InputStream (which MUST support mark and reset, or be a * PushbackInputStream) has a POIFS (OLE2) header at the start of it. If your InputStream does not * support mark / reset, then wrap it in a PushBackInputStream, then be sure to always use that, * and not the original! * * @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream */ public static boolean hasPOIFSHeader(InputStream inp) throws IOException { // We want to peek at the first 8 bytes inp.mark(8); byte[] header = new byte[8]; IOUtils.readFully(inp, header); LongField signature = new LongField(HeaderBlockConstants._signature_offset, header); // Wind back those 8 bytes if (inp instanceof PushbackInputStream) { PushbackInputStream pin = (PushbackInputStream) inp; pin.unread(header); } else { inp.reset(); } // Did it match the signature? return (signature.get() == HeaderBlockConstants._signature); }
/** * Generates a certificate object and initializes it with the data read from the input stream * inStream. */ public java.security.cert.Certificate engineGenerateCertificate(InputStream in) throws CertificateException { if (currentStream == null) { currentStream = in; sData = null; sDataObjectCount = 0; } else if (currentStream != in) // reset if input stream has changed { currentStream = in; sData = null; sDataObjectCount = 0; } try { if (sData != null) { if (sDataObjectCount != sData.size()) { return getCertificate(); } else { sData = null; sDataObjectCount = 0; return null; } } PushbackInputStream pis = new PushbackInputStream(in); int tag = pis.read(); if (tag == -1) { return null; } pis.unread(tag); if (tag != 0x30) // assume ascii PEM encoded. { return readPEMCertificate(pis); } else { return readDERCertificate(new ASN1InputStream(pis)); } } catch (Exception e) { throw new ExCertificateException(e); } }
private ProxyMessage readMsg(InputStream in) throws IOException { PushbackInputStream push_in; if (in instanceof PushbackInputStream) push_in = (PushbackInputStream) in; else push_in = new PushbackInputStream(in); int version = push_in.read(); push_in.unread(version); ProxyMessage msg; if (version == 5) { msg = new Socks5Message(push_in, false); } else if (version == 4) { msg = new Socks4Message(push_in, false); } else { throw new SocksException(CProxy.SOCKS_FAILURE); } return msg; }
@Override InputStream negotiateIncomingStream(Packet streamInitiation) throws XMPPException, InterruptedException { // build SOCKS5 Bytestream request Socks5BytestreamRequest request = new ByteStreamRequest(this.manager, (Bytestream) streamInitiation); // always accept the request Socks5BytestreamSession session = request.accept(); // test input stream try { PushbackInputStream stream = new PushbackInputStream(session.getInputStream()); int firstByte = stream.read(); stream.unread(firstByte); return stream; } catch (IOException e) { throw new XMPPException("Error establishing input stream", e); } }
/** * Takens an InputStream, verifies that it's not RTF, builds a POIFSFileSystem from it, and * returns that. */ public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException { // Open a PushbackInputStream, so we can peek at the first few bytes PushbackInputStream pis = new PushbackInputStream(istream, 6); byte[] first6 = new byte[6]; pis.read(first6); // Does it start with {\rtf ? If so, it's really RTF if (first6[0] == '{' && first6[1] == '\\' && first6[2] == 'r' && first6[3] == 't' && first6[4] == 'f') { throw new IllegalArgumentException("The document is really a RTF file"); } // OK, so it's not RTF // Open a POIFSFileSystem on the (pushed back) stream pis.unread(first6); return new POIFSFileSystem(pis); }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String str = "123456.654321"; ByteArrayInputStream b = new ByteArrayInputStream(str.getBytes()); PushbackInputStream p = null; p = new PushbackInputStream(b); int temp = 0; while ((temp = p.read()) != -1) { if (temp != '.') { System.out.print((char) temp); } else { p.unread(temp); temp = p.read(); System.out.println(""); System.out.println((char) temp); // break; } } p.close(); }