use of java.io.FilterInputStream in project cassandra by apache.
the class GoogleCloudSnitch method gceApiCall.
String gceApiCall(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");
conn.setRequestProperty("Metadata-Flavor", "Google");
if (conn.getResponseCode() != 200)
throw new ConfigurationException("GoogleCloudSnitch was unable to execute the API call. Not a gce node?");
// Read the information.
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();
}
}
use of java.io.FilterInputStream in project jetty.project by eclipse.
the class JarResource method getInputStream.
/* ------------------------------------------------------------ */
@Override
public InputStream getInputStream() throws java.io.IOException {
checkConnection();
if (!_urlString.endsWith("!/"))
return new FilterInputStream(getInputStream(false)) {
@Override
public void close() throws IOException {
this.in = IO.getClosedStream();
}
};
URL url = new URL(_urlString.substring(4, _urlString.length() - 2));
InputStream is = url.openStream();
return is;
}
use of java.io.FilterInputStream in project hudson-2.x by hudson.
the class BinarySafeStream method wrap.
/**
* Decode binary safe stream.
*/
public static InputStream wrap(InputStream in) {
return new FilterInputStream(in) {
/**
* Place a part of the decoded triplet that hasn's read by the caller yet.
* We allocate four bytes because of the way we implement {@link #read(byte[], int, int)},
* which puts encoded base64 in the given array during the computation.
*/
final byte[] triplet = new byte[4];
/**
* Remaining number of valid data in {@link #triplet}.
* -1 to indicate EOF. Otherwise always 0-2.
* Valid data starts at <code>triplet[3-remaining]</code>
*/
int remaining = 0;
final byte[] qualtet = new byte[4];
int input = 0;
public int read() throws IOException {
if (remaining == 0) {
remaining = _read(triplet, 0, 3);
if (// adjust to right
0 < remaining && remaining < 3)
System.arraycopy(triplet, 0, triplet, 3 - remaining, remaining);
}
if (remaining == -1)
// EOF
return -1;
assert remaining > 0;
return ((int) triplet[3 - remaining--]) & 0xFF;
}
public int read(byte[] b, int off, int len) throws IOException {
// EOF
if (remaining == -1)
return -1;
if (len < 4) {
// not enough space to process encoded data in the given buffer, so revert to the read-by-char
int read = 0;
int ch;
while (len > 0 && (ch = read()) != -1) {
b[off++] = (byte) ch;
read++;
len--;
}
return read;
}
// first copy any remaining bytes in triplet to output
int l = Math.min(len, remaining);
if (l > 0) {
System.arraycopy(triplet, 3 - remaining, b, off, l);
off += l;
len -= l;
remaining = 0;
if (super.available() == 0)
// the next read() may block, so let's return now
return l;
if (len < 4)
// not enough space to call _read(). abort.
return l;
// otherwise try to read more
int r = _read(b, off, len);
if (r == -1)
return l;
else
return l + r;
}
return _read(b, off, len);
}
/**
* The same as {@link #read(byte[], int, int)} but the buffer must be
* longer than off+4,
*/
private int _read(byte[] b, int off, int len) throws IOException {
assert remaining == 0;
assert b.length >= off + 4;
int totalRead = 0;
// read in the rest
if (len > 0) {
// put the remaining data from previous run at the top.
if (input > 0)
System.arraycopy(qualtet, 0, b, off, input);
// for us to return any byte we need to at least read 4 bytes,
// so insist on getting four bytes at least. When stream is flushed
// we get extra '=' in the middle.
// l = # of total encoded bytes to be processed in this round
int l = input;
while (l < 4) {
int r = super.read(b, off + l, Math.max(len, 4) - l);
if (r == -1) {
if (l % 4 != 0)
throw new IOException("Unexpected stream termination");
if (l == 0)
// EOF, and no data to process
return -1;
}
l += r;
}
// we can only decode multiple of 4, so write back any remaining data to qualtet.
// this also updates 'input' correctly.
input = l % 4;
if (input > 0) {
System.arraycopy(b, off + l - input, qualtet, 0, input);
l -= input;
}
// now we just need to convert four at a time
assert l % 4 == 0;
for (int base = off; l > 0; l -= 4) {
// convert b[base...base+3] to b[off...off+2]
// note that the buffer can be overlapping
int c0 = DECODING_TABLE[b[base++]];
int c1 = DECODING_TABLE[b[base++]];
int c2 = DECODING_TABLE[b[base++]];
int c3 = DECODING_TABLE[b[base++]];
if (c0 < 0 || c1 < 0 || c2 < -1 || c3 < -1) {
// illegal input. note that '=' never shows up as 1st or 2nd char
// hence the check for the 1st half and 2nd half are different.
// now try to report what we saw.
// the remaining buffer is b[base-4 ... base-4+l]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(b, base - 4, l);
// plus we might be able to read more bytes from the underlying stream
int avail = super.available();
if (avail > 0) {
byte[] buf = new byte[avail];
baos.write(buf, 0, super.read(buf));
}
StringBuilder buf = new StringBuilder("Invalid encoded sequence encountered:");
for (byte ch : baos.toByteArray()) buf.append(String.format(" %02X", ch));
throw new IOException(buf.toString());
}
b[off++] = (byte) ((c0 << 2) | (c1 >> 4));
totalRead++;
if (c2 != -1) {
b[off++] = (byte) ((c1 << 4) | (c2 >> 2));
totalRead++;
if (c3 != -1) {
b[off++] = (byte) ((c2 << 6) | c3);
totalRead++;
}
}
}
}
return totalRead;
}
public int available() throws IOException {
// roughly speaking we got 3/4 of the underlying available bytes
return super.available() * 3 / 4;
}
};
}
use of java.io.FilterInputStream in project rest.li by linkedin.
the class AbstractDataSchemaResolver method parse.
/**
* Read an {@link InputStream} and parse the {@link InputStream} looking for the
* specified name.
*
* @param inputStream to parse.
* @param location of the input source.
* @param name to locate.
* @param errorMessageBuilder to append error messages to.
* @return the {@link NamedDataSchema} is found in the input stream, else return null.
*/
protected NamedDataSchema parse(InputStream inputStream, final DataSchemaLocation location, String name, StringBuilder errorMessageBuilder) {
NamedDataSchema schema = null;
PegasusSchemaParser parser = _parserFactory.create(_dependencyResolver);
parser.setLocation(location);
parser.parse(new FilterInputStream(inputStream) {
@Override
public String toString() {
return location.toString();
}
});
if (parser.hasError()) {
errorMessageBuilder.append("Error parsing ").append(location).append(" for \"").append(name).append("\".\n");
errorMessageBuilder.append(parser.errorMessageBuilder());
errorMessageBuilder.append("Done parsing ").append(location).append(".\n");
_badLocations.add(location);
} else {
DataSchema found = _nameToDataSchema.get(name);
if (found != null && found instanceof NamedDataSchema) {
schema = (NamedDataSchema) found;
}
}
return schema;
}
use of java.io.FilterInputStream in project remusic by aa112901.
the class HttpUtil method getFromCache.
public static FilterInputStream getFromCache(Context context, String url) throws Exception {
// File cacheDirectory = new File("/storage/emulated/0/Android/data/com.name.demo .dev/cache/HttpCache");
File cacheDirectory = context.getExternalCacheDir();
DiskLruCache cache = DiskLruCache.create(FileSystem.SYSTEM, cacheDirectory, 201105, 2, 1024 * 1024 * 30);
cache.flush();
String key = Util.md5Hex(url);
final DiskLruCache.Snapshot snapshot;
try {
snapshot = cache.get(key);
if (snapshot == null) {
return null;
}
} catch (IOException e) {
return null;
}
okio.Source source = snapshot.getSource(1);
BufferedSource metadata = Okio.buffer(source);
FilterInputStream bodyIn = new FilterInputStream(metadata.inputStream()) {
@Override
public void close() throws IOException {
snapshot.close();
super.close();
}
};
return bodyIn;
}
Aggregations