Refine search
@Override public int read() throws IOException { int c = streams[currentStreamIndex].read(); if (c < 0) { IOUtils.closeQuietly(streams[currentStreamIndex]); while (currentStreamIndex < streams.length-1) { currentStreamIndex++; int tmpC = streams[currentStreamIndex].read(); if (tmpC < 0) { IOUtils.closeQuietly(streams[currentStreamIndex]); } else { return tmpC; } } return -1; } return c; } }
/** * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from * @return the requested byte array * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) { IOUtils.copy(stream, byteStream); request.setEntity(new ByteArrayEntity(byteStream.toByteArray())); String replyMessage = IOUtils.toString(reply); if (response.getStatusLine().getStatusCode() == 200) { JSONObject jReply = (JSONObject) new JSONParser().parse(replyMessage);
public void run() { try { IOUtils.copy(stream, new NullOutputStream()); } catch (IOException e) { } finally { IOUtils.closeQuietly(stream); } } };
ByteArrayOutputStream stdErrOutputStream = new ByteArrayOutputStream(); IOUtils.copy(tempOutputFileInputStream, outputStream); IOUtils.closeQuietly(tikaInputStream); IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(stdErrOutputStream); if (process.exitValue() != 0) { throw new TikaException("There was an error executing the command line" + "\nExecutable Command:\n\n" + cmd + "\nExecutable Error:\n\n" + stdErrOutputStream.toString(UTF_8.name()));
public void run() { try { try { IOUtils.copy(input, output); } finally { output.close(); } } catch (Exception e) { exception = e; } }
/** * Translate the given text InputStream to and from the given languages. * @see org.apache.tika.language.translate.Translator * @param text The text to translate. * @param sourceLanguage The input text language (for example, "hi"). * @param targetLanguage The desired output language (for example, "fr"). * @return The translated text. If translation is unavailable (client keys not set), returns the same text back. */ public String translate(InputStream text, String sourceLanguage, String targetLanguage){ try { return translator.translate(IOUtils.toString(text), sourceLanguage, targetLanguage); } catch (Exception e){ throw new IllegalStateException("Error translating data.", e); } }
static String htmlHeader(String title) { String header = "<html>\n"; header += " <head>\n"; header += " <title>Migration report" + (StringUtils.isBlank(title) ? "" : ": " + title) + "</title>\n"; String style = null; try { String[] resources = ClasspathResourcesUtil.findResources("/report-generator/migration-report.css"); if (resources != null && resources.length > 0) { InputStream in = DefaultReportGenerator.class.getResourceAsStream(resources[0]); style = IOUtils.toString(in); IOUtils.closeQuietly(in); } } catch (IOException e) { log.error("Cannot load CSS style: "+e.getMessage()); log.debug("Cannot load CSS style.", e); } if (style!=null) { header += "<style>\n"; header += style; header += "</style>\n"; } header += " </head>\n"; header += " <body>\n"; header += " <h1>Migration report</h1>\n\n"; return header; }
byte[] content = IOUtils.toByteArray(fis); long stopTime = System.currentTimeMillis(); long totalReadTime = Math.max(1, stopTime - startTime); throw new IOFetchException(url, e); } finally { IOUtils.closeQuietly(fis);
/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the default character encoding of the platform. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input) throws IOException { InputStreamReader reader = new InputStreamReader(input, UTF_8); return readLines(reader); }
private static void benchmark(File file) throws Exception { if (file.isHidden()) { // ignore } else if (file.isFile()) { try (InputStream input = new FileInputStream(file)) { byte[] content = IOUtils.toByteArray(input); String type = tika.detect(new ByteArrayInputStream(content)); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { tika.detect(new ByteArrayInputStream(content)); } System.out.printf( Locale.ROOT, "%6dns per Tika.detect(%s) = %s%n", System.currentTimeMillis() - start, file, type); } } else if (file.isDirectory()) { for (File child : file.listFiles()) { benchmark(child); } } }
/** * Convert the specified CharSequence to an input stream, encoded as bytes * using the default character encoding of the platform. * * @param input the CharSequence to convert * @return an input stream * @since IO 2.0 */ public static InputStream toInputStream(CharSequence input) { return toInputStream(input.toString()); }
ByteArrayOutputStream stdErrOutputStream = new ByteArrayOutputStream(); IOUtils.copy(tempOutputFileInputStream, outputStream); IOUtils.closeQuietly(tikaInputStream); IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(stdErrOutputStream); if (process.exitValue() != 0) { throw new TikaException("There was an error executing the command line" + "\nExecutable Command:\n\n" + cmd + "\nExecutable Error:\n\n" + stdErrOutputStream.toString(UTF_8.name()));
public void run() { try { IOUtils.copy(stream, new NullOutputStream()); } catch (IOException e) { } finally { IOUtils.closeQuietly(stream); } } };
public void run() { OutputStream stdin = process.getOutputStream(); try { IOUtils.copy(stream, stdin); } catch (IOException e) { } } };
/** * Translate the given text InputStream to the given language, attempting to auto-detect the source language. * This does not close the stream, so the caller has the responsibility of closing it. * @see org.apache.tika.language.translate.Translator * @param text The text to translate. * @param targetLanguage The desired output language (for example, "en"). * @return The translated text. If translation is unavailable (client keys not set), returns the same text back. */ public String translate(InputStream text, String targetLanguage){ try { return translator.translate(IOUtils.toString(text), targetLanguage); } catch (Exception e){ throw new IllegalStateException("Error translating data.", e); } }
/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @param encoding the encoding to use, null means platform default * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input, String encoding) throws IOException { if (encoding == null) { return readLines(input); } else { InputStreamReader reader = new InputStreamReader(input, encoding); return readLines(reader); } }
@Test public void testDetectApplicationEnviHdr() throws Exception { InputStream iStream = MagicDetectorTest.class.getResourceAsStream( "/test-documents/ang20150420t182050_corr_v1e_img.hdr"); byte[] data = IOUtils.toByteArray(iStream); MediaType testMT = new MediaType("application", "envi.hdr"); Detector detector = new MagicDetector(testMT, data, null, false, 0, 0); // Deliberately prevent InputStream.read(...) from reading the entire // buffer in one go InputStream stream = new RestrictiveInputStream(data); assertEquals(testMT, detector.detect(stream, new Metadata())); }
/** * Convert the specified CharSequence to an input stream, encoded as bytes * using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * * @param input the CharSequence to convert * @param encoding the encoding to use, null means platform default * @throws IOException if the encoding is invalid * @return an input stream * @since IO 2.0 */ public static InputStream toInputStream(CharSequence input, String encoding) throws IOException { return toInputStream(input.toString(), encoding); }
/** * Get the contents of a <code>Reader</code> as a <code>byte[]</code> * using the default character encoding of the platform. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedReader</code>. * * @param input the <code>Reader</code> to read from * @return the requested byte array * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(Reader input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }