Search in sources :

Example 16 with FilterInputStream

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();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) HttpURLConnection(java.net.HttpURLConnection) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) DataInputStream(java.io.DataInputStream) URL(java.net.URL)

Example 17 with FilterInputStream

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;
}
Also used : FilterInputStream(java.io.FilterInputStream) FilterInputStream(java.io.FilterInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 18 with FilterInputStream

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;
        }
    };
}
Also used : FilterInputStream(java.io.FilterInputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 19 with FilterInputStream

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;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) FilterInputStream(java.io.FilterInputStream)

Example 20 with FilterInputStream

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;
}
Also used : FilterInputStream(java.io.FilterInputStream) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) File(java.io.File) BufferedSource(okio.BufferedSource)

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