Search in sources :

Example 11 with FilterInputStream

use of java.io.FilterInputStream in project jackrabbit by apache.

the class JcrUtils method readFile.

/**
     * Returns a stream for reading the contents of the file stored at the
     * given node. This method works with both on nt:file and nt:resource and
     * on any other similar node types, as it only looks for the jcr:data
     * property or a jcr:content child node.
     * <p>
     * The returned stream contains a reference to the underlying
     * {@link Binary} value instance that will be disposed when the stream
     * is closed. It is the responsibility of the caller to close the stream
     * once it is no longer needed.
     *
     * @since Apache Jackrabbit 2.3
     * @param node node to be read
     * @return stream for reading the file contents
     * @throws RepositoryException if the file can not be accessed
     */
public static InputStream readFile(Node node) throws RepositoryException {
    if (node.hasProperty(Property.JCR_DATA)) {
        Property data = node.getProperty(Property.JCR_DATA);
        final Binary binary = data.getBinary();
        return new FilterInputStream(binary.getStream()) {

            @Override
            public void close() throws IOException {
                super.close();
                binary.dispose();
            }
        };
    } else if (node.hasNode(Node.JCR_CONTENT)) {
        return readFile(node.getNode(Node.JCR_CONTENT));
    } else {
        throw new RepositoryException("Unable to read file node: " + node.getPath());
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) RepositoryException(javax.jcr.RepositoryException) Binary(javax.jcr.Binary) Property(javax.jcr.Property)

Example 12 with FilterInputStream

use of java.io.FilterInputStream in project voldemort by voldemort.

the class HdfsFetcherAdvancedTest method unGunzipFile.

private void unGunzipFile(String compressedFile, String decompressedFile) {
    byte[] buffer = new byte[1024];
    try {
        FileSystem fs = FileSystem.getLocal(new Configuration());
        FSDataInputStream fileIn = fs.open(new Path(compressedFile));
        FilterInputStream gZIPInputStream = new GZIPInputStream(fileIn);
        FileOutputStream fileOutputStream = new FileOutputStream(decompressedFile);
        int bytes_read;
        while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, bytes_read);
        }
        gZIPInputStream.close();
        fileOutputStream.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem) FileOutputStream(java.io.FileOutputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException)

Example 13 with FilterInputStream

use of java.io.FilterInputStream in project bnd by bndtools.

the class P2Impl method jarStream.

private InputStream jarStream(File f, String name) throws IOException {
    final JarFile jaf = new JarFile(f);
    ZipEntry entry = jaf.getEntry(name);
    final InputStream inputStream = jaf.getInputStream(entry);
    return new FilterInputStream(inputStream) {

        @Override
        public void close() throws IOException {
            jaf.close();
        }
    };
}
Also used : FilterInputStream(java.io.FilterInputStream) FilterInputStream(java.io.FilterInputStream) XZInputStream(org.tukaani.xz.XZInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile)

Example 14 with FilterInputStream

use of java.io.FilterInputStream in project cdap by caskdata.

the class BundleJarUtil method getEntry.

/**
   * Returns an {@link InputSupplier} for a given entry. This avoids unjar the whole file to just get one entry.
   * However, to get many entries, unjar would be more efficient. Also, the jar file is scanned every time the
   * {@link InputSupplier#getInput()} is invoked.
   *
   * @param jarLocation Location of the jar file.
   * @param entryName Name of the entry to fetch
   * @return An {@link InputSupplier}.
   */
public static InputSupplier<InputStream> getEntry(final Location jarLocation, final String entryName) throws IOException {
    Preconditions.checkArgument(jarLocation != null);
    Preconditions.checkArgument(entryName != null);
    final URI uri = jarLocation.toURI();
    // Small optimization if the location is local
    if ("file".equals(uri.getScheme())) {
        return new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                final JarFile jarFile = new JarFile(new File(uri));
                ZipEntry entry = jarFile.getEntry(entryName);
                if (entry == null) {
                    throw new IOException("Entry not found for " + entryName);
                }
                return new FilterInputStream(jarFile.getInputStream(entry)) {

                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                        } finally {
                            jarFile.close();
                        }
                    }
                };
            }
        };
    }
    // Otherwise, use JarInputStream
    return new InputSupplier<InputStream>() {

        @Override
        public InputStream getInput() throws IOException {
            JarInputStream is = new JarInputStream(jarLocation.getInputStream());
            JarEntry entry = is.getNextJarEntry();
            while (entry != null) {
                if (entryName.equals(entry.getName())) {
                    return is;
                }
                entry = is.getNextJarEntry();
            }
            Closeables.closeQuietly(is);
            throw new IOException("Entry not found for " + entryName);
        }
    };
}
Also used : FilterInputStream(java.io.FilterInputStream) JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) JarFile(java.util.jar.JarFile) File(java.io.File) InputSupplier(com.google.common.io.InputSupplier)

Example 15 with FilterInputStream

use of java.io.FilterInputStream in project cassandra by apache.

the class Ec2Snitch method awsApiCall.

String awsApiCall(String url) throws IOException, ConfigurationException {
    // Populate the region and zone by introspection, fail if 404 on metadata
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    DataInputStream d = null;
    try {
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() != 200)
            throw new ConfigurationException("Ec2Snitch was unable to execute the API call. Not an ec2 node?");
        // Read the information. I wish I could say (String) conn.getContent() here...
        int cl = conn.getContentLength();
        byte[] b = new byte[cl];
        d = new DataInputStream((FilterInputStream) conn.getContent());
        d.readFully(b);
        return new String(b, StandardCharsets.UTF_8);
    } finally {
        FileUtils.close(d);
        conn.disconnect();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) HttpURLConnection(java.net.HttpURLConnection) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) DataInputStream(java.io.DataInputStream) URL(java.net.URL)

Aggregations

FilterInputStream (java.io.FilterInputStream)26 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileOutputStream (java.io.FileOutputStream)4 URL (java.net.URL)4 ZipEntry (java.util.zip.ZipEntry)4 BufferedOutputStream (java.io.BufferedOutputStream)2 DataInputStream (java.io.DataInputStream)2 OutputStream (java.io.OutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 Random (java.util.Random)2 JarFile (java.util.jar.JarFile)2 JarInputStream (java.util.jar.JarInputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ZipInputStream (java.util.zip.ZipInputStream)2