public MediaType detect(InputStream stream, Metadata metadata) throws IOException { Key key = Pharmacy.getKey(); MediaType type = MediaType.OCTET_STREAM; try (InputStream lookahead = new LookaheadInputStream(stream, 1024)) { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, key); InputStream decrypted = new CipherInputStream(lookahead, cipher); QName name = new XmlRootExtractor().extractRootElement(decrypted); if (name != null && "http://example.com/xpd".equals(name.getNamespaceURI()) && "prescription".equals(name.getLocalPart())) { type = MediaType.application("x-prescription"); } } catch (GeneralSecurityException e) { // unable to decrypt, fall through } return type; } }
@Override public int read() throws IOException { fill(); if (buffered > position) { return 0xff & buffer[position++]; } else { return -1; } }
@Override public int read(byte[] b, int off, int len) throws IOException { fill(); if (buffered > position) { len = Math.min(len, buffered - position); System.arraycopy(buffer, position, b, off, len); position += len; return len; } else { return -1; } }
@Test public void testNullStream() throws IOException { InputStream lookahead = new LookaheadInputStream(null, 100); assertEquals(-1, lookahead.read()); }
@Override public int read() throws IOException { fill(); if (buffered > position) { return 0xff & buffer[position++]; } else { return -1; } }
@Test public void testEmptyStream() throws IOException { InputStream stream = new ByteArrayInputStream(new byte[0]); InputStream lookahead = new LookaheadInputStream(stream, 100); assertEquals(-1, lookahead.read()); lookahead.close(); assertEquals(-1, stream.read()); }
@Override public int read() throws IOException { fill(); if (buffered > position) { return 0xff & buffer[position++]; } else { return -1; } }
@Test public void testZeroLookahead() throws IOException { InputStream stream = new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }); InputStream lookahead = new LookaheadInputStream(stream, 0); assertEquals(-1, lookahead.read()); lookahead.close(); assertEquals('a', stream.read()); assertEquals('b', stream.read()); assertEquals('c', stream.read()); assertEquals(-1, stream.read()); }
@Override public int read(byte[] b, int off, int len) throws IOException { fill(); if (buffered > position) { len = Math.min(len, buffered - position); System.arraycopy(buffer, position, b, off, len); position += len; return len; } else { return -1; } }
@Test public void testBasicLookahead() throws IOException { InputStream stream = new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }); InputStream lookahead = new LookaheadInputStream(stream, 2); assertEquals('a', lookahead.read()); assertEquals('b', lookahead.read()); assertEquals(-1, lookahead.read()); lookahead.close(); assertEquals('a', stream.read()); assertEquals('b', stream.read()); assertEquals('c', stream.read()); assertEquals(-1, stream.read()); }
@Override public int read(byte[] b, int off, int len) throws IOException { fill(); if (buffered > position) { len = Math.min(len, buffered - position); System.arraycopy(buffer, position, b, off, len); position += len; return len; } else { return -1; } }
@Test public void testSkipLookahead() throws IOException { InputStream stream = new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }); InputStream lookahead = new LookaheadInputStream(stream, 2); assertEquals(1, lookahead.skip(1)); assertEquals('b', lookahead.read()); assertEquals(0, lookahead.skip(1)); assertEquals(-1, lookahead.read()); lookahead.close(); assertEquals('a', stream.read()); assertEquals('b', stream.read()); assertEquals('c', stream.read()); assertEquals(-1, stream.read()); }
@Test public void testMarkLookahead() throws IOException { InputStream stream = new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }); InputStream lookahead = new LookaheadInputStream(stream, 2); lookahead.mark(1); assertEquals('a', lookahead.read()); lookahead.reset(); assertEquals('a', lookahead.read()); lookahead.mark(2); assertEquals('b', lookahead.read()); assertEquals(-1, lookahead.read()); lookahead.reset(); assertEquals('b', lookahead.read()); assertEquals(-1, lookahead.read()); lookahead.close(); assertEquals('a', stream.read()); assertEquals('b', stream.read()); assertEquals('c', stream.read()); assertEquals(-1, stream.read()); }