use of java.io.SequenceInputStream in project LibreraReader by foobnix.
the class ZipFile method getInputStream.
/**
* Returns an InputStream for reading the contents of the given entry.
*
* @param ze the entry to get the stream for.
* @return a stream to read the entry from.
* @throws IOException if unable to create an input stream from the zipentry
*/
public InputStream getInputStream(final ZipArchiveEntry ze) throws IOException {
if (!(ze instanceof Entry)) {
return null;
}
// cast validity is checked just above
ZipUtil.checkRequestedFeatures(ze);
final long start = ze.getDataOffset();
// doesn't get closed if the method is not supported - which
// should never happen because of the checkRequestedFeatures
// call above
final InputStream is = // NOSONAR
new BufferedInputStream(createBoundedInputStream(start, ze.getCompressedSize()));
switch(ZipMethod.getMethodByCode(ze.getMethod())) {
case STORED:
return is;
case UNSHRINKING:
return new UnshrinkingInputStream(is);
case IMPLODING:
return new ExplodingInputStream(ze.getGeneralPurposeBit().getSlidingDictionarySize(), ze.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), is);
case DEFLATED:
final Inflater inflater = new Inflater(true);
// https://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html#Inflater(boolean)
return new InflaterInputStream(new SequenceInputStream(is, new ByteArrayInputStream(ONE_ZERO_BYTE)), inflater) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
inflater.end();
}
}
};
case BZIP2:
return new BZip2CompressorInputStream(is);
case ENHANCED_DEFLATED:
return new Deflate64CompressorInputStream(is);
case AES_ENCRYPTED:
case EXPANDING_LEVEL_1:
case EXPANDING_LEVEL_2:
case EXPANDING_LEVEL_3:
case EXPANDING_LEVEL_4:
case JPEG:
case LZMA:
case PKWARE_IMPLODING:
case PPMD:
case TOKENIZATION:
case UNKNOWN:
case WAVPACK:
case XZ:
default:
throw new ZipException("Found unsupported compression method " + ze.getMethod());
}
}
use of java.io.SequenceInputStream in project imageio-ext by geosolutions-it.
the class GZIPImageInputStream method readTrailer.
/**
* Reads GZIP member trailer.
*/
private void readTrailer() throws IOException {
InputStream in = new InputStreamAdapter(this.iis);
int n = inf.getRemaining();
if (n > 0) {
in = new SequenceInputStream(new ByteArrayInputStream(buf, len - n, n), in);
}
// Uses left-to-right evaluation order
if ((readUInt(in) != crc.getValue()) || // should be preferred)
(readUInt(in) != (inf.getTotalOut() & 0xffffffffL)))
throw new IOException("Corrupt GZIP trailer");
}
use of java.io.SequenceInputStream in project serverless by bluenimble.
the class DefaultScriptingEngine method eval.
@Override
public Object eval(Supported supported, final Api api, ApiResource resource, ScriptContext sContext) throws ScriptingEngineException {
if (supported == null) {
throw new ScriptingEngineException("Unsupported Scripting Engine ");
}
ScriptEngine engine = engines.get(supported);
if (engine == null) {
throw new ScriptingEngineException("Unsupported Scripting Engine " + supported);
}
// get platform libs
ScriptObjectMirror libs = (ScriptObjectMirror) platform.get(Libs);
String[] libsKeys = libs.getOwnKeys(true);
CachedScript cached = scripts.get(resource.owner() + Lang.COLON + resource.path());
Reader reader = null;
if (cached == null || cached.timestamp < resource.timestamp().getTime()) {
InputStream rio = null;
try {
StringBuilder startScript = new StringBuilder(ScriptStart);
// add platform libraries
for (String lib : libsKeys) {
startScript.append(Var).append(Lang.SPACE).append(lib).append(Lang.EQUALS).append(Libs).append(Lang.DOT).append(lib).append(Lang.SEMICOLON);
}
startScript.append(Native);
for (String d : Denied) {
startScript.append(d);
}
String sStartScript = startScript.toString();
startScript.setLength(0);
rio = resource.toInput();
// format String script = platform.callMember (, arg);
List<InputStream> blocks = new ArrayList<InputStream>();
blocks.add(new ByteArrayInputStream(sStartScript.getBytes()));
blocks.add(rio);
blocks.add(new ByteArrayInputStream(ScriptEnd.getBytes()));
reader = new InputStreamReader(new SequenceInputStream(Collections.enumeration(blocks)));
cached = new CachedScript();
cached.script = ((Compilable) engine).compile(reader);
cached.timestamp = resource.timestamp().getTime();
scripts.put(resource.owner() + Lang.COLON + resource.path(), cached);
} catch (Exception e) {
throw new ScriptingEngineException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(rio);
}
}
if (sContext == null) {
return null;
}
Bindings bindings = new SimpleBindings();
bindings.put(JavaClass, new Function<String, Class<?>>() {
@Override
public Class<?> apply(String type) {
try {
return api.getClassLoader().loadClass(type);
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException(cnfe);
}
}
});
// add platform libraries
bindings.put(Libs, libs);
try {
Iterator<String> vars = sContext.vars();
while (vars.hasNext()) {
String var = vars.next();
bindings.put(var, sContext.var(var));
}
return cached.script.eval(bindings);
} catch (ScriptException e) {
throw new ScriptingEngineException(e.getMessage(), e);
} finally {
bindings.clear();
}
}
use of java.io.SequenceInputStream in project android-common by litesuits.
the class ByteArrayOutputStream method toBufferedInputStream.
/**
* Gets the current contents of this byte stream as a Input Stream. The
* returned stream is backed by buffers of <code>this</code> stream,
* avoiding memory allocation and copy, thus saving space and time.<br>
*
* @return the current contents of this output stream.
* @see java.io.ByteArrayOutputStream#toByteArray()
* @see #reset()
* @since 2.0
*/
private InputStream toBufferedInputStream() {
int remaining = count;
if (remaining == 0) {
return new ClosedInputStream();
}
List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size());
for (byte[] buf : buffers) {
int c = Math.min(buf.length, remaining);
list.add(new ByteArrayInputStream(buf, 0, c));
remaining -= c;
if (remaining == 0) {
break;
}
}
return new SequenceInputStream(Collections.enumeration(list));
}
use of java.io.SequenceInputStream in project databus by linkedin.
the class StaxBuilderTest method processXml.
public void processXml() throws Exception {
try {
// wrap the stream with fake root
String xmlStartTag = "<?xml version=\"" + DbusConstants.XML_VERSION + "\" encoding=\"" + DbusConstants.ISO_8859_1 + "\"?><root>";
String xmlEndTag = "</root>";
List xmlTagsList = Arrays.asList(new InputStream[] { new ByteArrayInputStream(xmlStartTag.getBytes(DbusConstants.ISO_8859_1)), _fileInputStream, new ByteArrayInputStream(xmlEndTag.getBytes(DbusConstants.ISO_8859_1)) });
Enumeration<InputStream> streams = Collections.enumeration(xmlTagsList);
SequenceInputStream seqStream = new SequenceInputStream(streams);
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(seqStream);
// TODO replace _tableToNamespace this by physical sources config
XmlParser parser = new XmlParser(xmlStreamReader, _schemaRegistry, _tableToNamespace, _tableToSourceId, _transactionSuccessCallBack, true, _staticReplicationConfig, seqStream);
parser.start();
} catch (XMLStreamException e) {
LOG.error("Unable to parse the given xml stream", e);
}
}
Aggregations