List of usage examples for java.io InputStream reset
public synchronized void reset() throws IOException
mark
method was last called on this input stream. From source file:org.apache.sling.distribution.packaging.impl.DistributionPackageUtils.java
public static void readInfo(InputStream inputStream, Map<String, Object> info) { try {/*from ww w . j a v a2s . co m*/ int size = META_START.getBytes("UTF-8").length; inputStream.mark(size); byte[] buffer = new byte[size]; int bytesRead = inputStream.read(buffer, 0, size); String s = new String(buffer, "UTF-8"); if (bytesRead > 0 && buffer[0] > 0 && META_START.equals(s)) { ObjectInputStream stream = getSafeObjectInputStream(inputStream); HashMap<String, Object> map = (HashMap<String, Object>) stream.readObject(); info.putAll(map); } else { inputStream.reset(); } } catch (IOException e) { log.error("Cannot read stream info", e); } catch (ClassNotFoundException e) { log.error("Cannot read stream info", e); } }
From source file:org.apache.tika.parser.pkg.TikaCompressorStreamFactory.java
/** * Try to detect the type of compressor stream. * * @param in input stream/*from www. j a v a 2s . c om*/ * @return type of compressor stream detected * @throws CompressorException if no compressor stream type was detected * or if something else went wrong * @throws IllegalArgumentException if stream is null or does not support mark * * @since 1.14 */ public static String detect(final InputStream in) throws CompressorException { if (in == null) { throw new IllegalArgumentException("Stream must not be null."); } if (!in.markSupported()) { throw new IllegalArgumentException("Mark is not supported."); } final byte[] signature = new byte[12]; in.mark(signature.length); int signatureLength = -1; try { signatureLength = IOUtils.readFully(in, signature); in.reset(); } catch (IOException e) { throw new CompressorException("IOException while reading signature.", e); } if (BZip2CompressorInputStream.matches(signature, signatureLength)) { return BZIP2; } if (GzipCompressorInputStream.matches(signature, signatureLength)) { return GZIP; } if (Pack200CompressorInputStream.matches(signature, signatureLength)) { return PACK200; } if (FramedSnappyCompressorInputStream.matches(signature, signatureLength)) { return SNAPPY_FRAMED; } if (ZCompressorInputStream.matches(signature, signatureLength)) { return Z; } if (DeflateCompressorInputStream.matches(signature, signatureLength)) { return DEFLATE; } if (XZUtils.matches(signature, signatureLength)) { return XZ; } if (LZMAUtils.matches(signature, signatureLength)) { return LZMA; } /* if (FramedLZ4CompressorInputStream.matches(signature, signatureLength)) { return LZ4_FRAMED; }*/ throw new CompressorException("No Compressor found for the stream signature."); }
From source file:com.zimbra.common.util.ByteUtil.java
/** * Determines if the data in the given stream is gzipped. * Requires that the <tt>InputStream</tt> supports mark/reset. */// w w w. j a v a 2s.co m public static boolean isGzipped(InputStream in) throws IOException { in.mark(2); int header = in.read() | (in.read() << 8); in.reset(); if (header == GZIPInputStream.GZIP_MAGIC) { return true; } return false; }
From source file:org.apache.synapse.commons.json.JsonUtil.java
/** * Returns a reusable cached copy of the JSON stream contained in the provided Message Context. * * @param messageContext Axis2 Message context that contains a JSON payload. * @return {@link java.io.InputStream}// w w w . ja v a 2 s . c om */ private static InputStream cachedCopyOfJsonPayload(MessageContext messageContext) { if (messageContext == null) { logger.error("#cachedCopyOfJsonPayload. Cannot copy JSON stream from message context. [null]."); return null; } InputStream jsonStream = jsonStream(messageContext, true); if (jsonStream == null) { logger.error("#cachedCopyOfJsonPayload. Cannot copy JSON stream from message context. [null] stream."); return null; } String inputStreamCache = Long.toString(jsonStream.hashCode()); Object o = messageContext.getProperty(inputStreamCache); if (o instanceof InputStream) { InputStream inputStream = (InputStream) o; try { inputStream.reset(); if (logger.isDebugEnabled()) { logger.debug("#cachedCopyOfJsonPayload. Cache HIT"); } return inputStream; } catch (IOException e) { logger.warn("#cachedCopyOfJsonPayload. Could not reuse the cached input stream. Error>>> " + e.getLocalizedMessage()); } } org.apache.commons.io.output.ByteArrayOutputStream out = new org.apache.commons.io.output.ByteArrayOutputStream(); try { IOUtils.copy(jsonStream, out); out.flush(); InputStream inputStream = toReadOnlyStream(new ByteArrayInputStream(out.toByteArray())); messageContext.setProperty(inputStreamCache, inputStream); if (logger.isDebugEnabled()) { logger.debug("#cachedCopyOfJsonPayload. Cache MISS"); } return inputStream; } catch (IOException e) { logger.error("#cachedCopyOfJsonPayload. Could not copy the JSON stream from message context. Error>>> " + e.getLocalizedMessage()); } return null; }
From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java
/** * Try to determine the type of Archiver * @param in input stream/*from w w w . j a v a2s .c o m*/ * @return type of archiver if found * @throws ArchiveException if an archiver cannot be detected in the stream * @since 1.14 */ public static String detect(InputStream in) throws ArchiveException { if (in == null) { throw new IllegalArgumentException("Stream must not be null."); } if (!in.markSupported()) { throw new IllegalArgumentException("Mark is not supported."); } final byte[] signature = new byte[SIGNATURE_SIZE]; in.mark(signature.length); int signatureLength = -1; try { signatureLength = IOUtils.readFully(in, signature); in.reset(); } catch (IOException e) { throw new ArchiveException("IOException while reading signature."); } if (ZipArchiveInputStream.matches(signature, signatureLength)) { return ZIP; } else if (JarArchiveInputStream.matches(signature, signatureLength)) { return JAR; } if (ArArchiveInputStream.matches(signature, signatureLength)) { return AR; } else if (CpioArchiveInputStream.matches(signature, signatureLength)) { return CPIO; } else if (ArjArchiveInputStream.matches(signature, signatureLength)) { return ARJ; } else if (SevenZFile.matches(signature, signatureLength)) { return SEVEN_Z; } // Dump needs a bigger buffer to check the signature; final byte[] dumpsig = new byte[DUMP_SIGNATURE_SIZE]; in.mark(dumpsig.length); try { signatureLength = IOUtils.readFully(in, dumpsig); in.reset(); } catch (IOException e) { throw new ArchiveException("IOException while reading dump signature"); } if (DumpArchiveInputStream.matches(dumpsig, signatureLength)) { return DUMP; } // Tar needs an even bigger buffer to check the signature; read the first block final byte[] tarHeader = new byte[TAR_HEADER_SIZE]; in.mark(tarHeader.length); try { signatureLength = IOUtils.readFully(in, tarHeader); in.reset(); } catch (IOException e) { throw new ArchiveException("IOException while reading tar signature"); } if (TarArchiveInputStream.matches(tarHeader, signatureLength)) { return TAR; } // COMPRESS-117 - improve auto-recognition if (signatureLength >= TAR_HEADER_SIZE) { TarArchiveInputStream tais = null; try { tais = new TarArchiveInputStream(new ByteArrayInputStream(tarHeader)); // COMPRESS-191 - verify the header checksum if (tais.getNextTarEntry().isCheckSumOK()) { return TAR; } } catch (final Exception e) { // NOPMD // can generate IllegalArgumentException as well // as IOException // autodetection, simply not a TAR // ignored } finally { IOUtils.closeQuietly(tais); } } throw new ArchiveException("No Archiver found for the stream signature"); }
From source file:org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService.java
@Override public YangStoreSnapshot getYangStoreSnapshot() throws YangStoreException { for (InputStream inputStream : byteArrayInputStreams) { try {/* w w w . jav a2 s .co m*/ inputStream.reset(); } catch (IOException e) { throw new RuntimeException(e); } } return new MbeParser().parseYangFiles(byteArrayInputStreams); }
From source file:org.openehealth.ipf.platform.camel.flow.render.SimpleMessageRenderer.java
private String render(InputStream stream) { try {/*from w ww . j av a 2s . c o m*/ String result = IOUtils.toString(stream); stream.reset(); return result; } catch (IOException e) { return null; } }
From source file:com.ibm.og.client.CustomHttpEntity.java
@Override // FIXME getContent is supposed to return a new instance of InputStream if isRepeatable is true - // is it safe to simply reset the stream? public InputStream getContent() throws IOException, IllegalStateException { this.requestContentStart = 0; this.requestContentFinish = 0; final InputStream content = this.request.getContent(); content.reset(); return content; }
From source file:org.apache.nifi.processors.standard.util.crypto.CipherUtility.java
public static byte[] readBytesFromInputStream(InputStream in, String label, int limit, byte[] delimiter) throws IOException, ProcessException { if (in == null) { throw new IllegalArgumentException("Cannot read " + label + " from null InputStream"); }//from w w w . j a va2s. c o m // If the value is not detected within the first n bytes, throw an exception in.mark(limit); // The first n bytes of the input stream contain the value up to the custom delimiter ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); byte[] stoppedBy = StreamUtils.copyExclusive(in, bytesOut, limit + delimiter.length, delimiter); if (stoppedBy != null) { byte[] bytes = bytesOut.toByteArray(); return bytes; } // If no delimiter was found, reset the cursor in.reset(); return null; }
From source file:org.opendaylight.controller.netconf.it.HardcodedYangStoreService.java
@Override public YangStoreSnapshot getYangStoreSnapshot() throws YangStoreException { for (InputStream inputStream : byteArrayInputStreams) { try {/*from w w w .j av a 2 s . com*/ inputStream.reset(); } catch (IOException e) { throw new RuntimeException(e); } } YangParserImpl yangParser = new YangParserImpl(); final SchemaContext schemaContext = yangParser.resolveSchemaContext( new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(byteArrayInputStreams).values())); YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() { @Override public SchemaContext getSchemaContext() { return schemaContext; } }); return yangStoreService.getYangStoreSnapshot(); }