Search in sources :

Example 31 with SequenceInputStream

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());
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ZipException(java.util.zip.ZipException) SequenceInputStream(java.io.SequenceInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Inflater(java.util.zip.Inflater) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream)

Example 32 with SequenceInputStream

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");
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStreamAdapter(it.geosolutions.io.input.adapter.InputStreamAdapter) IOException(java.io.IOException)

Example 33 with SequenceInputStream

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();
    }
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) SequenceInputStream(java.io.SequenceInputStream) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleBindings(javax.script.SimpleBindings)

Example 34 with SequenceInputStream

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));
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList)

Example 35 with SequenceInputStream

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);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SequenceInputStream(java.io.SequenceInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) List(java.util.List) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

SequenceInputStream (java.io.SequenceInputStream)119 InputStream (java.io.InputStream)76 ByteArrayInputStream (java.io.ByteArrayInputStream)65 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)31 FileInputStream (java.io.FileInputStream)22 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Test (org.junit.Test)10 Vector (java.util.Vector)9 lombok.val (lombok.val)9 OutputStream (java.io.OutputStream)8 List (java.util.List)8 FileOutputStream (java.io.FileOutputStream)7 InputStreamReader (java.io.InputStreamReader)7 HashMap (java.util.HashMap)7 File (java.io.File)6 ByteBuffer (java.nio.ByteBuffer)6 Support_ASimpleInputStream (tests.support.Support_ASimpleInputStream)6 Reader (java.io.Reader)5