Java Code Examples for java.io.FilterInputStream

The following are top voted examples for showing how to use java.io.FilterInputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: guava-mock   File: MultiInputStreamTest.java   View source code 6 votes vote down vote up
public void testOnlyOneOpen() throws Exception {
  final ByteSource source = newByteSource(0, 50);
  final int[] counter = new int[1];
  ByteSource checker = new ByteSource() {
    @Override
    public InputStream openStream() throws IOException {
      if (counter[0]++ != 0) {
        throw new IllegalStateException("More than one source open");
      }
      return new FilterInputStream(source.openStream()) {
        @Override public void close() throws IOException {
          super.close();
          counter[0]--;
        }
      };
    }
  };
  byte[] result = ByteSource.concat(checker, checker, checker).read();
  assertEquals(150, result.length);
}
 
Example 2
Project: OpenJSharp   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}
 
Example 3
Project: OpenJSharp   File: ClassReader.java   View source code 6 votes vote down vote up
ClassReader(Class cls, InputStream in) throws IOException {
    this.pkg = cls.getPackage();
    this.cls = cls;
    this.verbose = pkg.verbose;
    this.in = new DataInputStream(new FilterInputStream(in) {
        public int read(byte b[], int off, int len) throws IOException {
            int nr = super.read(b, off, len);
            if (nr >= 0)  inPos += nr;
            return nr;
        }
        public int read() throws IOException {
            int ch = super.read();
            if (ch >= 0)  inPos += 1;
            return ch;
        }
        public long skip(long n) throws IOException {
            long ns = super.skip(n);
            if (ns >= 0)  inPos += ns;
            return ns;
        }
    });
}
 
Example 4
Project: OpenJSharp   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 5
Project: OpenJSharp   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 6
Project: jdk8u-jdk   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}
 
Example 7
Project: jdk8u-jdk   File: ClassReader.java   View source code 6 votes vote down vote up
ClassReader(Class cls, InputStream in) throws IOException {
    this.pkg = cls.getPackage();
    this.cls = cls;
    this.verbose = pkg.verbose;
    this.in = new DataInputStream(new FilterInputStream(in) {
        public int read(byte b[], int off, int len) throws IOException {
            int nr = super.read(b, off, len);
            if (nr >= 0)  inPos += nr;
            return nr;
        }
        public int read() throws IOException {
            int ch = super.read();
            if (ch >= 0)  inPos += 1;
            return ch;
        }
        public long skip(long n) throws IOException {
            long ns = super.skip(n);
            if (ns >= 0)  inPos += ns;
            return ns;
        }
    });
}
 
Example 8
Project: jdk8u-jdk   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 9
Project: openjdk-jdk10   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}
 
Example 10
Project: openjdk-jdk10   File: ClassReader.java   View source code 6 votes vote down vote up
ClassReader(Class cls, InputStream in) throws IOException {
    this.pkg = cls.getPackage();
    this.cls = cls;
    this.verbose = pkg.verbose;
    this.in = new DataInputStream(new FilterInputStream(in) {
        public int read(byte b[], int off, int len) throws IOException {
            int nr = super.read(b, off, len);
            if (nr >= 0)  inPos += nr;
            return nr;
        }
        public int read() throws IOException {
            int ch = super.read();
            if (ch >= 0)  inPos += 1;
            return ch;
        }
        public long skip(long n) throws IOException {
            long ns = super.skip(n);
            if (ns >= 0)  inPos += ns;
            return ns;
        }
    });
}
 
Example 11
Project: openjdk-jdk10   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 12
Project: googles-monorepo-demo   File: MultiInputStreamTest.java   View source code 6 votes vote down vote up
public void testOnlyOneOpen() throws Exception {
  final ByteSource source = newByteSource(0, 50);
  final int[] counter = new int[1];
  ByteSource checker = new ByteSource() {
    @Override
    public InputStream openStream() throws IOException {
      if (counter[0]++ != 0) {
        throw new IllegalStateException("More than one source open");
      }
      return new FilterInputStream(source.openStream()) {
        @Override public void close() throws IOException {
          super.close();
          counter[0]--;
        }
      };
    }
  };
  byte[] result = ByteSource.concat(checker, checker, checker).read();
  assertEquals(150, result.length);
}
 
Example 13
Project: sctalk   File: GifLoadTask.java   View source code 6 votes vote down vote up
private FilterInputStream getFromCache(String url) throws Exception {
    DiskLruCache cache = DiskLruCache.open(CommonUtil.getImageSavePath(), 1, 2, 2*1024*1024);
    cache.flush();
    String key = Util.hash(url);
    final DiskLruCache.Snapshot snapshot;
    try {
        snapshot = cache.get(key);
        if (snapshot == null) {
            return null;
        }
    } catch (IOException e) {
        return null;
    }
    FilterInputStream bodyIn = new FilterInputStream(snapshot.getInputStream(1)) {
        @Override
        public void close() throws IOException {
            snapshot.close();
            super.close();
        }
    };
    return bodyIn;
}
 
Example 14
Project: openjdk9   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}
 
Example 15
Project: openjdk9   File: ClassReader.java   View source code 6 votes vote down vote up
ClassReader(Class cls, InputStream in) throws IOException {
    this.pkg = cls.getPackage();
    this.cls = cls;
    this.verbose = pkg.verbose;
    this.in = new DataInputStream(new FilterInputStream(in) {
        public int read(byte b[], int off, int len) throws IOException {
            int nr = super.read(b, off, len);
            if (nr >= 0)  inPos += nr;
            return nr;
        }
        public int read() throws IOException {
            int ch = super.read();
            if (ch >= 0)  inPos += 1;
            return ch;
        }
        public long skip(long n) throws IOException {
            long ns = super.skip(n);
            if (ns >= 0)  inPos += ns;
            return ns;
        }
    });
}
 
Example 16
Project: openjdk9   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 17
Project: dw-jdbc   File: FileBackedInputStreamTest.java   View source code 6 votes vote down vote up
@Test
public void testDownloadException() throws Exception {
    byte[] content = genSampleBytes(8000);
    // Simulate the socket being closed before all expected bytes were downloaded.
    InputStream contentIn = new FilterInputStream(new ByteArrayInputStream(content)) {
        @Override
        public int read(@Nonnull byte[] b, int off, int len) throws IOException {
            int count = super.read(b, off, len);
            if (count == -1) {
                throw new EOFException("Unexpected end of input");
            }
            return count;
        }
    };
    try (InputStream in = new FileBackedInputStream(contentIn, 0, newCachedExecutor())) {
        //noinspection ResultOfMethodCallIgnored
        assertThatThrownBy(() -> in.skip(10000))
                .isExactlyInstanceOf(IOException.class)
                .hasMessage("Unexpected end of input")
                .hasCauseExactlyInstanceOf(EOFException.class);
    }
}
 
Example 18
Project: goib   File: EClientSocket.java   View source code 6 votes vote down vote up
private synchronized void eDisconnect( boolean resetState ) {
    // not connected?
    if( m_dis == null & m_socketTransport == null) {
        return;
    }

    if ( resetState ) {
    	m_connected = false;
        m_extraAuth = false;
        m_clientId = -1;
        m_serverVersion = 0;
        m_TwsTime = "";
        m_redirectCount = 0;
    }

    FilterInputStream dis = m_dis;
    m_dis = null;
    m_socketTransport = null;

    try {
        if (dis != null)
        	dis.close();
    }
    catch( Exception e) {
    }
}
 
Example 19
Project: FoDBugTrackerUtility   File: ApacheConnector.java   View source code 6 votes vote down vote up
private static InputStream getInputStream(final CloseableHttpResponse response) throws IOException {

        final InputStream inputStream;

        if (response.getEntity() == null) {
            inputStream = new ByteArrayInputStream(new byte[0]);
        } else {
            final InputStream i = response.getEntity().getContent();
            if (i.markSupported()) {
                inputStream = i;
            } else {
                inputStream = new BufferedInputStream(i, ReaderWriter.BUFFER_SIZE);
            }
        }

        return new FilterInputStream(inputStream) {
            @Override
            public void close() throws IOException {
                response.close();
                super.close();
            }
        };
    }
 
Example 20
Project: jdk8u_jdk   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}
 
Example 21
Project: jdk8u_jdk   File: ClassReader.java   View source code 6 votes vote down vote up
ClassReader(Class cls, InputStream in) throws IOException {
    this.pkg = cls.getPackage();
    this.cls = cls;
    this.verbose = pkg.verbose;
    this.in = new DataInputStream(new FilterInputStream(in) {
        public int read(byte b[], int off, int len) throws IOException {
            int nr = super.read(b, off, len);
            if (nr >= 0)  inPos += nr;
            return nr;
        }
        public int read() throws IOException {
            int ch = super.read();
            if (ch >= 0)  inPos += 1;
            return ch;
        }
        public long skip(long n) throws IOException {
            long ns = super.skip(n);
            if (ns >= 0)  inPos += ns;
            return ns;
        }
    });
}
 
Example 22
Project: jdk8u_jdk   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 23
Project: lookaside_java-1.8.0-openjdk   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}
 
Example 24
Project: lookaside_java-1.8.0-openjdk   File: ClassReader.java   View source code 6 votes vote down vote up
ClassReader(Class cls, InputStream in) throws IOException {
    this.pkg = cls.getPackage();
    this.cls = cls;
    this.verbose = pkg.verbose;
    this.in = new DataInputStream(new FilterInputStream(in) {
        public int read(byte b[], int off, int len) throws IOException {
            int nr = super.read(b, off, len);
            if (nr >= 0)  inPos += nr;
            return nr;
        }
        public int read() throws IOException {
            int ch = super.read();
            if (ch >= 0)  inPos += 1;
            return ch;
        }
        public long skip(long n) throws IOException {
            long ns = super.skip(n);
            if (ns >= 0)  inPos += ns;
            return ns;
        }
    });
}
 
Example 25
Project: lookaside_java-1.8.0-openjdk   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 26
Project: fcrepo-api-x   File: HttpRegistry.java   View source code 6 votes vote down vote up
InputStream getStream() {
    try {
        if (!isClosed && response.getEntity().isRepeatable()) {
            return response.getEntity().getContent();
        } else {
            getResponse();
            return new FilterInputStream(response.getEntity().getContent()) {

                @Override
                public void close() throws IOException {
                    isClosed = true;
                    super.close();
                }
            };
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 27
Project: intellij-ce-playground   File: URLUtil.java   View source code 6 votes vote down vote up
@NotNull
private static InputStream openJarStream(@NotNull URL url) throws IOException {
  Pair<String, String> paths = splitJarUrl(url.getFile());
  if (paths == null) {
    throw new MalformedURLException(url.getFile());
  }

  @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") final ZipFile zipFile = new ZipFile(FileUtil.unquote(paths.first));
  ZipEntry zipEntry = zipFile.getEntry(paths.second);
  if (zipEntry == null) {
    zipFile.close();
    throw new FileNotFoundException("Entry " + paths.second + " not found in " + paths.first);
  }

  return new FilterInputStream(zipFile.getInputStream(zipEntry)) {
    @Override
    public void close() throws IOException {
      super.close();
      zipFile.close();
    }
  };
}
 
Example 28
Project: cassandra-kmean   File: GoogleCloudSnitch.java   View source code 6 votes vote down vote up
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();
    }
}
 
Example 29
Project: cassandra-kmean   File: Ec2Snitch.java   View source code 6 votes vote down vote up
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();
    }
}
 
Example 30
Project: aesh-readline   File: LineDisciplineTerminal.java   View source code 6 votes vote down vote up
public LineDisciplineTerminal(String name,
                              String type,
                              OutputStream masterOutput) throws IOException {
    super(name, type);
    PipedInputStream input = new LinePipedInputStream(PIPE_SIZE);
    this.slaveInputPipe = new PipedOutputStream(input);
    // This is a hack to fix a problem in gogo where closure closes
    // streams for commands if they are instances of PipedInputStream.
    // So we need to get around and make sure it's not an instance of
    // that class by using a dumb FilterInputStream class to wrap it.
    this.slaveInput = new FilterInputStream(input) {};
    this.slaveOutput = new FilteringOutputStream();
    this.masterOutput = masterOutput;
    this.attributes = new Attributes();
    this.size = new Size(160, 50);
}
 
Example 31
Project: lasta-di   File: ResourceTraversal.java   View source code 6 votes vote down vote up
public static void forEach(final ZipInputStream zipInputStream, final String prefix, final ResourceHandler handler) {
    final int pos = prefix.length();
    ZipEntry entry = null;
    while ((entry = LdiZipInputStreamUtil.getNextEntry(zipInputStream)) != null) {
        if (!entry.isDirectory()) {
            final String entryName = entry.getName().replace('\\', '/');
            if (!entryName.startsWith(prefix)) {
                continue;
            }
            handler.processResource(entryName.substring(pos), new FilterInputStream(zipInputStream) {
                public void close() throws IOException {
                    LdiZipInputStreamUtil.closeEntry(zipInputStream);
                }
            });
        }
    }
}
 
Example 32
Project: IoTgo_Android_App   File: JarResource.java   View source code 6 votes vote down vote up
@Override
public InputStream getInputStream()
    throws java.io.IOException
{     
    checkConnection();
    if (!_urlString.endsWith("!/"))
        return new FilterInputStream(super.getInputStream()) 
        {
            @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;
}
 
Example 33
Project: IoTgo_Android_App   File: JarResource.java   View source code 6 votes vote down vote up
@Override
public InputStream getInputStream()
    throws java.io.IOException
{     
    checkConnection();
    if (!_urlString.endsWith("!/"))
        return new FilterInputStream(super.getInputStream()) 
        {
            @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;
}
 
Example 34
Project: ACaZoo   File: Ec2Snitch.java   View source code 6 votes vote down vote up
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, Charsets.UTF_8);
    }
    finally
    {
        FileUtils.close(d);
        conn.disconnect();
    }
}
 
Example 35
Project: guava-libraries   File: MultiInputStreamTest.java   View source code 6 votes vote down vote up
public void testOnlyOneOpen() throws Exception {
  final ByteSource source = newByteSource(0, 50);
  final int[] counter = new int[1];
  ByteSource checker = new ByteSource() {
    @Override
    public InputStream openStream() throws IOException {
      if (counter[0]++ != 0) {
        throw new IllegalStateException("More than one source open");
      }
      return new FilterInputStream(source.openStream()) {
        @Override public void close() throws IOException {
          super.close();
          counter[0]--;
        }
      };
    }
  };
  byte[] result = ByteSource.concat(checker, checker, checker).read();
  assertEquals(150, result.length);
}
 
Example 36
Project: daq-eclipse   File: FTPBlobDownloadStrategy.java   View source code 6 votes vote down vote up
public InputStream getInputStream(ActiveMQBlobMessage message) throws IOException, JMSException {
    url = message.getURL();
    final FTPClient ftp = createFTP();
    String path = url.getPath();
    String workingDir = path.substring(0, path.lastIndexOf("/"));
    String file = path.substring(path.lastIndexOf("/") + 1);
    ftp.changeWorkingDirectory(workingDir);
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

    InputStream input = new FilterInputStream(ftp.retrieveFileStream(file)) {

        public void close() throws IOException {
            in.close();
            ftp.quit();
            ftp.disconnect();
        }
    };

    return input;
}
 
Example 37
Project: infobip-open-jdk-8   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}
 
Example 38
Project: infobip-open-jdk-8   File: ClassReader.java   View source code 6 votes vote down vote up
ClassReader(Class cls, InputStream in) throws IOException {
    this.pkg = cls.getPackage();
    this.cls = cls;
    this.verbose = pkg.verbose;
    this.in = new DataInputStream(new FilterInputStream(in) {
        public int read(byte b[], int off, int len) throws IOException {
            int nr = super.read(b, off, len);
            if (nr >= 0)  inPos += nr;
            return nr;
        }
        public int read() throws IOException {
            int ch = super.read();
            if (ch >= 0)  inPos += 1;
            return ch;
        }
        public long skip(long n) throws IOException {
            long ns = super.skip(n);
            if (ns >= 0)  inPos += ns;
            return ns;
        }
    });
}
 
Example 39
Project: infobip-open-jdk-8   File: PackageReader.java   View source code 6 votes vote down vote up
LimitedBuffer(InputStream originalIn) {
    super(null, 1<<14);
    servedPos = pos;
    super.in = new FilterInputStream(originalIn) {
        public int read() throws IOException {
            if (buffered == limit)
                return -1;
            ++buffered;
            return super.read();
        }
        public int read(byte b[], int off, int len) throws IOException {
            if (buffered == limit)
                return -1;
            if (limit != -1) {
                long remaining = limit - buffered;
                if (len > remaining)
                    len = (int)remaining;
            }
            int nr = super.read(b, off, len);
            if (nr >= 0)  buffered += nr;
            return nr;
        }
    };
}
 
Example 40
Project: jdk8u-dev-jdk   File: BandStructure.java   View source code 6 votes vote down vote up
public void setInputStreamFrom(InputStream in) throws IOException {
    assert(bytes == null);
    assert(assertReadyToReadFrom(this, in));
    setPhase(READ_PHASE);
    this.in = in;
    if (optDumpBands) {
        // Tap the stream.
        bytesForDump = new ByteArrayOutputStream();
        this.in = new FilterInputStream(in) {
            @Override
            public int read() throws IOException {
                int ch = in.read();
                if (ch >= 0)  bytesForDump.write(ch);
                return ch;
            }
            @Override
            public int read(byte b[], int off, int len) throws IOException {
                int nr = in.read(b, off, len);
                if (nr >= 0)  bytesForDump.write(b, off, nr);
                return nr;
            }
        };
    }
    super.readyToDisburse();
}