@Override protected InputStream getResourceInputStream(final Resource resource, final String entityId) throws IOException { if (resource instanceof UrlResource && resource.getURL().toExternalForm().toLowerCase().endsWith("/entities/")) { final String encodedId = EncodingUtils.urlEncode(entityId); final URL url = new URL(resource.getURL().toExternalForm().concat(encodedId)); LOGGER.debug("Locating metadata input stream for [{}] via [{}]", encodedId, url); final HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); httpcon.setDoOutput(true); httpcon.addRequestProperty("Accept", "*/*"); httpcon.setRequestMethod("GET"); httpcon.connect(); return httpcon.getInputStream(); } return ClosedInputStream.CLOSED_INPUT_STREAM; }
@Test @SuppressWarnings("unchecked") public void testPumpInClosed() { final InputStream in = new ClosedInputStream(); final EventListener<String> nl = context.mock(EventListener.class, "nl"); final EventListener<Integer> pl = context.mock(EventListener.class, "pl"); final EventListener<IOException> el = context.mock(EventListener.class, "el"); context.checking(new Expectations() { { never(nl).receive(with(any(Object.class)), with(any(String.class))); never(pl).receive(with(any(Object.class)), with(any(Integer.class))); never(el).receive(with(any(Object.class)), with(any(IOException.class))); } }); final TileProgressPump p = new TileProgressPump(nl, pl, el); p.setInputStream(in); p.run(); }
@Test @SuppressWarnings("unchecked") public void testPumpInClosed() { final InputStream in = new ClosedInputStream(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final EventListener<IOException> el = context.mock(EventListener.class); context.checking(new Expectations() { { never(el).receive(with(any(Object.class)), with(any(IOException.class))); } }); final InputOutputStreamPump p = new InputOutputStreamPump(in, out, el); p.run(); assertArrayEquals(new byte[0], out.toByteArray()); }
@Test @SuppressWarnings("unchecked") public void testPumpBothClosed() { final InputStream in = new ClosedInputStream(); final OutputStream out = new ClosedOutputStream(); final EventListener<IOException> el = context.mock(EventListener.class); context.checking(new Expectations() { { never(el).receive(with(any(Object.class)), with(any(IOException.class))); } }); final InputOutputStreamPump p = new InputOutputStreamPump(in, out, el); p.run(); }
/** * Gets the current contents of this byte stream as a Input Stream. The * returned stream is backed by buffers of <code>this</code> stream, * avoiding memory allocation and copy, thus saving space and time.<br> * * @return the current contents of this output stream. * @see java.io.ByteArrayOutputStream#toByteArray() * @see #reset() * @since Commons IO 2.0 */ private InputStream toBufferedInputStream() { int remaining = count; if (remaining == 0) { return new ClosedInputStream(); } List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size()); for (byte[] buf : buffers) { int c = Math.min(buf.length, remaining); list.add(new ByteArrayInputStream(buf, 0, c)); remaining -= c; if (remaining == 0) { break; } } return new SequenceInputStream(Collections.enumeration(list)); }
/** * Gets the current contents of this byte stream as a Input Stream. The * returned stream is backed by buffers of <code>this</code> stream, * avoiding memory allocation and copy, thus saving space and time.<br> * * @return the current contents of this output stream. * @see java.io.ByteArrayOutputStream#toByteArray() * @see #reset() * @since 2.0 */ private InputStream toBufferedInputStream() { int remaining = count; if (remaining == 0) { return new ClosedInputStream(); } List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size()); for (byte[] buf : buffers) { int c = Math.min(buf.length, remaining); list.add(new ByteArrayInputStream(buf, 0, c)); remaining -= c; if (remaining == 0) { break; } } return new SequenceInputStream(Collections.enumeration(list)); }
/** * Gets the current contents of this byte stream as a Input Stream. The * returned stream is backed by buffers of <code>this</code> stream, * avoiding memory allocation and copy, thus saving space and time.<br> * * @return the current contents of this output stream. * @see java.io.ByteArrayOutputStream#toByteArray() * @see #reset() * @since 2.5 */ public synchronized InputStream toInputStream() { int remaining = count; if (remaining == 0) { return new ClosedInputStream(); } final List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size()); for (final byte[] buf : buffers) { final int c = Math.min(buf.length, remaining); list.add(new ByteArrayInputStream(buf, 0, c)); remaining -= c; if (remaining == 0) { break; } } reuseBuffers = false; return new SequenceInputStream(Collections.enumeration(list)); }
@Test(expected=UnsupportedOperationException.class) public void testSetInputStreamRunning() { final TileProgressPump p = new TPP(); p.run(); final InputStream in = new ClosedInputStream(); p.setInputStream(in); }
@Test(expected=UnsupportedOperationException.class) public void testSetInputStreamRunning() { final InputOutputStreamPump p = new IOSP(); p.run(); final InputStream in = new ClosedInputStream(); p.setInputStream(in); }
@Test public void testCloseQuietlyImageInputStreamClosed() throws IOException { final ImageInputStream in = new MemoryCacheImageInputStream(new ClosedInputStream()); in.close(); IOUtils.closeQuietly(in); }
@Override public void close() throws IOException { if (delegate != null) { delegate.close(); } //forget the reference to original inputstream to potentially free some memory delegate = ClosedInputStream.CLOSED_INPUT_STREAM; }
@Test public void testSetInputStreamNotRunning() { final TileProgressPump p = new TPP(); final InputStream in = new ClosedInputStream(); p.setInputStream(in); }
@Test public void testSetInputStreamNotRunning() { final InputOutputStreamPump p = new InputOutputStreamPump(); final InputStream in = new ClosedInputStream(); p.setInputStream(in); }
@Override public InputSource resolveEntity(String publicId, String systemId) { return new InputSource(ClosedInputStream.CLOSED_INPUT_STREAM); }
@Override public InputStream resolveEntity(String publicId, String systemId, String baseURI, String namespace) { return ClosedInputStream.CLOSED_INPUT_STREAM; }
public boolean isClosed() { return delegate == ClosedInputStream.CLOSED_INPUT_STREAM; }