Search in sources :

Example 6 with FilterInputStream

use of java.io.FilterInputStream in project intellij-community by JetBrains.

the class TestAnonymousParams method foo.

void foo(InputStream in, int a) throws IOException {
    FilterInputStream filterInputStream = new FilterInputStream(in) {

        @Override
        public int read() throws IOException {
            return a;
        }
    };
    filterInputStream.read();
}
Also used : FilterInputStream(java.io.FilterInputStream)

Example 7 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 8 with FilterInputStream

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

the class DatabaseFileSystem method getInputStream.

/**
     * {@inheritDoc}
     */
public InputStream getInputStream(String filePath) throws FileSystemException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    FileSystemPathUtil.checkFormat(filePath);
    String parentDir = FileSystemPathUtil.getParentDir(filePath);
    String name = FileSystemPathUtil.getName(filePath);
    synchronized (selectDataSQL) {
        try {
            final ResultSet rs = conHelper.exec(selectDataSQL, new Object[] { parentDir, name }, false, 0);
            if (!rs.next()) {
                throw new FileSystemException("no such file: " + filePath);
            }
            InputStream in = rs.getBinaryStream(1);
            /**
                 * return an InputStream wrapper in order to
                 * close the ResultSet when the stream is closed
                 */
            return new FilterInputStream(in) {

                public void close() throws IOException {
                    super.close();
                    // close ResultSet
                    DbUtility.close(rs);
                }
            };
        } catch (SQLException e) {
            String msg = "failed to retrieve data of file: " + filePath;
            log.error(msg, e);
            throw new FileSystemException(msg, e);
        }
    }
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) FilterInputStream(java.io.FilterInputStream) SQLException(java.sql.SQLException) FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet)

Example 9 with FilterInputStream

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

the class StatsCollectingStreams method wrap.

public static InputStream wrap(final BlobStatsCollector collector, final String blobId, InputStream in) {
    final CountingInputStream cin = new CountingInputStream(in);
    return new FilterInputStream(cin) {

        final long startTime = System.nanoTime();

        @Override
        public void close() throws IOException {
            super.close();
            //We rely on close to determine how much was downloaded
            //as once an InputStream is exposed its not possible to
            //determine if the stream is actually used
            //Download time might not be accurate as reading code might
            //be processing also as it moved further in stream. So that
            //overhead would add to the download time
            collector.downloaded(blobId, System.nanoTime() - startTime, TimeUnit.NANOSECONDS, cin.getCount());
            collector.downloadCompleted(blobId);
        }
    };
}
Also used : FilterInputStream(java.io.FilterInputStream) CountingInputStream(com.google.common.io.CountingInputStream)

Example 10 with FilterInputStream

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

the class IOUtilsTest method testReadFully.

public void testReadFully() throws IOException {
    final Random r = new Random(1);
    byte[] data = new byte[1000];
    final AtomicInteger readCount = new AtomicInteger();
    r.nextBytes(data);
    FilterInputStream in = new FilterInputStream(new ByteArrayInputStream(data)) {

        @Override
        public int read(byte[] buffer, int off, int max) throws IOException {
            readCount.incrementAndGet();
            if (r.nextInt(10) == 0) {
                return 0;
            }
            return in.read(buffer, off, Math.min(10, max));
        }
    };
    in.mark(10000);
    byte[] test = new byte[1000];
    // readFully is not supposed to call read when reading 0 bytes
    assertEquals(0, IOUtils.readFully(in, test, 0, 0));
    assertEquals(0, readCount.get());
    assertEquals(1000, IOUtils.readFully(in, test, 0, 1000));
    IOUtilsTest.assertEquals(data, test);
    test = new byte[1001];
    in.reset();
    in.mark(10000);
    assertEquals(1000, IOUtils.readFully(in, test, 0, 1001));
    assertEquals(0, IOUtils.readFully(in, test, 0, 0));
}
Also used : FilterInputStream(java.io.FilterInputStream) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream)

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