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();
}
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();
}
}
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);
}
}
}
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);
}
};
}
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));
}
Aggregations