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