public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { TemporaryResources tmp = new TemporaryResources(); try { TikaInputStream tis = TikaInputStream.get(stream, tmp); parse(tis, handler, metadata, context); } finally { tmp.dispose(); } }
TemporaryResources tmp = new TemporaryResources(); File rotationScript = tmp.createTemporaryFile(); Files.copy(in, rotationScript.toPath(), StandardCopyOption.REPLACE_EXISTING); tmp.close();
/** * for testing purposes; this method write the histogram vector to a file. * * @param histogram * @throws IOException */ private void writeHisto(final float[] histogram) throws IOException { Path histPath = new TemporaryResources().createTempFile(); try (Writer writer = Files.newBufferedWriter(histPath, UTF_8)) { for (float bin : histogram) { writer.write(String.valueOf(bin) + "\t"); // writer.write(i + "\t"); } writer.write("\r\n"); } }
@Override public void close() throws IOException { path = null; mark = -1; // The close method was explicitly called, so we indeed // are expected to close the input stream. Handle that // by adding that stream as a resource to be tracked before // closing all of them. This way also possible exceptions from // the close() calls get managed properly. tmp.addResource(in); tmp.close(); }
public void parse(Image image, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { TemporaryResources tmp = new TemporaryResources(); FileOutputStream fos = null; TikaInputStream tis = null; try { int w = image.getWidth(null); int h = image.getHeight(null); BufferedImage bImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); File file = tmp.createTemporaryFile(); fos = new FileOutputStream(file); ImageIO.write(bImage, "png", fos); tis = TikaInputStream.get(file); parse(tis, handler, metadata, context); } finally { tmp.dispose(); if (tis != null) tis.close(); if (fos != null) fos.close(); } }
TemporaryResources tmp = new TemporaryResources(); try { Path tmpFile = tmp.createTempFile(); try (OutputStream os = Files.newOutputStream(tmpFile)) { throw new IOExceptionWithCause("error writing OCR content from PDF", e); } finally { tmp.dispose();
TemporaryResources tmp = new TemporaryResources(); TikaInputStream tstream = TikaInputStream.get(stream, tmp); tmp.addResource(isoFile); tmp.dispose();
/** * Creates a TikaInputStream instance. This private constructor is used * by the static factory methods based on the available information. * * @param path the path to the file that contains the stream * @throws IOException if an I/O error occurs */ private TikaInputStream(Path path) throws IOException { super(new BufferedInputStream(Files.newInputStream(path))); this.path = path; this.tmp = new TemporaryResources(); this.length = Files.size(path); }
public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { TemporaryResources tmp = null; try { if (!TikaInputStream.isTikaInputStream(stream)) { tmp = new TemporaryResources(); stream = TikaInputStream.get(stream, tmp); } //AutoDetectReader can throw exceptions during //initialization. If we just created a //TemporaryResources, we need to make sure to close it. parseImpl(stream, handler, metadata, context); } finally { if (tmp != null) { tmp.close(); } } }
tempOutputFile = tmp.createTemporaryFile(); commandSegment = commandSegment.replace( ExternalParser.OUTPUT_FILE_TOKEN, sendStdOutToOutputStream(process, outputStream); } else { tmp.dispose(); try { process.waitFor();
static boolean hasPython() { // check if python is installed and it has the required dependencies for the rotation program to run boolean hasPython = false; TemporaryResources tmp = null; try { tmp = new TemporaryResources(); File importCheck = tmp.createTemporaryFile(); String prg = "import numpy, matplotlib, skimage, _tkinter"; OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(importCheck), Charset.forName("UTF-8")); out.write(prg); out.close(); Process p = Runtime.getRuntime().exec("python " + importCheck.getAbsolutePath()); if (p.waitFor() == 0) { hasPython = true; } } catch (Exception e) { } finally { IOUtils.closeQuietly(tmp); } return hasPython; }
public Path getPath() throws IOException { if (path == null) { if (position > 0) { throw new IOException("Stream is already being read"); } else { // Spool the entire stream into a temporary file path = tmp.createTempFile(); Files.copy(in, path, REPLACE_EXISTING); // Create a new input stream and make sure it'll get closed InputStream newStream = Files.newInputStream(path); tmp.addResource(newStream); // Replace the spooled stream with the new stream in a way // that still ends up closing the old stream if or when the // close() method is called. The closing of the new stream // is already being handled as noted above. final InputStream oldStream = in; in = new BufferedInputStream(newStream) { @Override public void close() throws IOException { oldStream.close(); } }; length = Files.size(path); } } return path; }
/** * 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); } }
/** * Creates and returns a temporary file that will automatically be * deleted when the {@link #close()} method is called. * * @return Created temporary file that'll be deleted after closing * @throws IOException * @see #createTempFile() */ public File createTemporaryFile() throws IOException { return createTempFile().toFile(); }
/** * Calls the {@link #close()} method and wraps the potential * {@link IOException} into a {@link TikaException} for convenience * when used within Tika. * * @throws TikaException if one or more of the tracked resources * could not be closed */ public void dispose() throws TikaException { try { close(); } catch (IOException e) { throw new TikaException("Failed to close temporary resources", e); } }
output = tmp.createTemporaryFile(); outputFromStdOut = false; cmd[i] = cmd[i].replace(OUTPUT_FILE_TOKEN, output.getPath());
/** * Use this to parse content without starting a new document. * This appends SAX events to xhtml without re-adding the metadata, body start, etc. * * @param stream inputstream * @param xhtml handler * @param config TesseractOCRConfig to use for this parse * @throws IOException * @throws SAXException * @throws TikaException * */ public void parseInline(InputStream stream, XHTMLContentHandler xhtml, ParseContext parseContext, TesseractOCRConfig config) throws IOException, SAXException, TikaException { // If Tesseract is not on the path with the current config, do not try to run OCR // getSupportedTypes shouldn't have listed us as handling it, so this should only // occur if someone directly calls this parser, not via DefaultParser or similar if (! hasTesseract(config)) return; TemporaryResources tmp = new TemporaryResources(); try { TikaInputStream tikaStream = TikaInputStream.get(stream, tmp); File tmpImgFile = tmp.createTemporaryFile(); parse(tikaStream, tmpImgFile, parseContext, xhtml, config); } finally { tmp.dispose(); } }
TemporaryResources tmp = new TemporaryResources(); try { BufferedImage image = renderer.renderImage(pageIndex, 2.0f, config.getOcrImageType()); Path tmpFile = tmp.createTempFile(); try (OutputStream os = Files.newOutputStream(tmpFile)) { throw new IOExceptionWithCause("error writing OCR content from PDF", e); } finally { tmp.dispose();
TemporaryResources tmp = new TemporaryResources(); TikaInputStream tstream = TikaInputStream.get(stream, tmp); tmp.addResource(isoFile); tmp.dispose();
/** * Creates a TikaInputStream instance. This private constructor is used * by the static factory methods based on the available information. * * @param file the file that contains the stream * @throws FileNotFoundException if the file does not exist * @deprecated use {@link #TikaInputStream(Path)} */ @Deprecated private TikaInputStream(File file) throws FileNotFoundException { super(new BufferedInputStream(new FileInputStream(file))); this.path = file.toPath(); this.tmp = new TemporaryResources(); this.length = file.length(); }