@Override public int available() throws IOException { try { return stream.available(); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public InputStream getStream() throws RepositoryException { try { return new SelfClosingInputStream(internalStream()); } catch (RepositoryException re) { throw re; } catch (Exception e) { throw new RepositoryException(e); } }
@Override public String mimeTypeOf( final String name, final Binary binaryValue ) throws RepositoryException, IOException { Metadata metadata = new Metadata(); if (!StringUtil.isBlank(name)) { metadata.set(Metadata.RESOURCE_NAME_KEY, name); } if (binaryValue == null) { return name == null ? null : detect(null, metadata); } InputStream stream = binaryValue.getStream(); if (stream instanceof SelfClosingInputStream) { //we need to avoid the SelfClosingInputStream because Tika will read and mark from this stream multiple times stream = ((SelfClosingInputStream)stream).wrappedStream(); } return detect(stream, metadata); }
@Override public InputStream getStream() throws RepositoryException { try { return new SelfClosingInputStream(internalStream()); } catch (RepositoryException re) { throw re; } catch (Exception e) { throw new RepositoryException(e); } }
@Override public String mimeTypeOf( final String name, final Binary binaryValue ) throws RepositoryException, IOException { Metadata metadata = new Metadata(); if (!StringUtil.isBlank(name)) { metadata.set(Metadata.RESOURCE_NAME_KEY, name); } if (binaryValue == null) { return name == null ? null : detect(null, metadata); } InputStream stream = binaryValue.getStream(); if (stream instanceof SelfClosingInputStream) { //we need to avoid the SelfClosingInputStream because Tika will read and mark from this stream multiple times stream = ((SelfClosingInputStream)stream).wrappedStream(); } return detect(stream, metadata); }
@Override public void reset() throws IOException { try { stream.reset(); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Test @FixFor("MODE-2413") public void shouldSupportReadingFromSelfClosingInputStream() throws Exception { byte[] buffer = new byte[1024]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); URL resource = getClass().getResource("simple.json"); try (Base64.InputStream is = new Base64.InputStream(new SelfClosingInputStream(resource.openStream()), Base64.ENCODE)) { int read; while ((read = is.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, read); } } // until Java 8, use this.... String expectedString = DatatypeConverter.printBase64Binary(IoUtil.readBytes(resource.openStream())); assertEquals("Incorrect Base64 encoding", expectedString, new String(bos.toByteArray())); } }
@Override public int available() throws IOException { try { return stream.available(); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public void reset() throws IOException { try { stream.reset(); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public long skip( long n ) throws IOException { try { return stream.skip(n); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public void reset() throws IOException { try { stream.reset(); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public long skip( long n ) throws IOException { try { return stream.skip(n); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public int available() throws IOException { try { return stream.available(); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public long skip( long n ) throws IOException { try { return stream.skip(n); } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public int read( byte[] b ) throws IOException { try { int result = stream.read(b); if (result == -1) { // the end of the stream has been reached ... closeStream(); } return result; } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public void mark( int readlimit ) { try { stream.mark(readlimit); } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public void mark( int readlimit ) { try { stream.mark(readlimit); } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public int read( byte[] b ) throws IOException { try { int result = stream.read(b); if (result == -1) { // the end of the stream has been reached ... closeStream(); } return result; } catch (IOException e) { closeStream(); throw e; } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public boolean markSupported() { try { return stream.markSupported(); } catch (RuntimeException e) { closeStream(); throw e; } }
@Override public void mark( int readlimit ) { try { stream.mark(readlimit); } catch (RuntimeException e) { closeStream(); throw e; } }