} finally { if (tikaIS != null) { tikaIS.close();
/** * Creates a TikaInputStream from a Factory which can create * fresh {@link InputStream}s for the same resource multiple times. * <p>This is typically desired when working with {@link Parser}s that * need to re-read the stream multiple times, where other forms * of buffering (eg File) are slower than just getting a fresh * new stream each time. */ public static TikaInputStream get(InputStreamFactory factory) throws IOException { return get(factory, new TemporaryResources()); } /**
public void run() { try { IOUtils.copy(stream, new NullOutputStream()); } catch (IOException e) { } finally { IOUtils.closeQuietly(stream); } } };
private Map<String, String> tika_parse(InputStream sourceStream, String prefix, Integer maxAttribs, Integer maxAttribLen) throws IOException, TikaException, SAXException { final Metadata metadata = new Metadata(); final TikaInputStream tikaInputStream = TikaInputStream.get(sourceStream); try { autoDetectParser.parse(tikaInputStream, new DefaultHandler(), metadata); } finally { tikaInputStream.close();
@Override public InputStream openInputStream() throws IOException { //no need to include Metadata because we already set the //same information in the initializer return TikaInputStream.get(fullPath); } }
/** * Creates a TikaInputStream from a Factory which can create * fresh {@link InputStream}s for the same resource multiple times. * <p>This is typically desired when working with {@link Parser}s that * need to re-read the stream multiple times, where other forms * of buffering (eg File) are slower than just getting a fresh * new stream each time. */ public static TikaInputStream get(InputStreamFactory factory, TemporaryResources tmp) throws IOException { TikaInputStream stream = get(factory.getInputStream(), tmp); stream.steamFactory = factory; return stream; }
/** * Stores the open container object against * the stream, eg after a Zip contents * detector has loaded the file to decide * what it contains. */ public void setOpenContainer(Object container) { openContainer = container; if (container instanceof Closeable) { tmp.addResource((Closeable) container); } }
/** * Get a LE short value from an InputStream * * @param stream the InputStream from which the short is to be read * @return the short (16-bit) value * @throws IOException will be propagated back to the caller * @throws BufferUnderrunException if the stream cannot provide enough bytes */ public static short readShortLE(InputStream stream) throws IOException, BufferUnderrunException { return (short) readUShortLE(stream); }
/** * Get a LE short value from a byte array * * @param data the byte array * @param offset a starting offset into the byte array * @return the short (16-bit) value */ public static short getShortLE(byte[] data, int offset) { return (short) getUShortLE(data, offset); }
/** * Get a BE short value from an InputStream * * @param stream the InputStream from which the short is to be read * @return the short (16-bit) value * @throws IOException will be propagated back to the caller * @throws BufferUnderrunException if the stream cannot provide enough bytes */ public static short readShortBE(InputStream stream) throws IOException, BufferUnderrunException { return (short) readUShortBE(stream); }
/** * Casts or wraps the given stream to a TaggedInputStream instance. * * @param proxy normal input stream * @return a TaggedInputStream instance */ public static TaggedInputStream get(InputStream proxy) { if(proxy instanceof TaggedInputStream) { return (TaggedInputStream)proxy; } return new TaggedInputStream(proxy); }
@Override public void mark(int readlimit) { super.mark(readlimit); mark = position; }
/** * Get a BE short value from the beginning of a byte array * * @param data the byte array * @return the short (16-bit) value */ public static short getShortBE(byte[] data) { return getShortBE(data, 0); }
/** * Get a LE unsigned int value from a byte array * * @param data the byte array * @return the unsigned int (32-bit) value in a long */ public static long getUIntLE(byte[] data) { return getUIntLE(data, 0); }
@Override public int read() throws IOException { fill(); if (buffered > position) { return 0xff & buffer[position++]; } else { return -1; } }
/** * Replaces the underlying input stream with a {@link ClosedInputStream} * sentinel. The original input stream will remain open, but this proxy * will appear closed. */ @Override public void close() { in = new ClosedInputStream(); }
/** * Get a LE short value from the beginning of a byte array * * @param data the byte array * @return the short (16-bit) value */ public static short getShortLE(byte[] data) { return getShortLE(data, 0); }
/** * Get a LE int value from the beginning of a byte array * * @param data the byte array * @return the int (32-bit) value */ public static int getIntLE(byte[] data) { return getIntLE(data, 0); }
/** * Get a BE unsigned int value from a byte array * * @param data the byte array * @param offset a starting offset into the byte array * @return the unsigned int (32-bit) value in a long */ public static long getUIntBE(byte[] data, int offset) { long retNum = getIntBE(data, offset); return retNum & 0x00FFFFFFFFl; }
@Override public void process(final InputStream stream) throws IOException { try (final InputStream in = new BufferedInputStream(stream)) { TikaInputStream tikaStream = TikaInputStream.get(in); Metadata metadata = new Metadata(); if (filename != null && context.getProperty(USE_FILENAME_IN_DETECTION).asBoolean()) { metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, filename); } // Get mime type MediaType mediatype = detector.detect(tikaStream, metadata); mimeTypeRef.set(mediatype.toString()); } } });