Tabnine Logo
IOUtils
Code IndexAdd Tabnine to your IDE (free)

How to use
IOUtils
in
org.apache.commons.io

Best Java code snippets using org.apache.commons.io.IOUtils (Showing top 20 results out of 40,293)

Refine searchRefine arrow

  • FileOutputStream
  • FileInputStream
  • InputStream
  • URL
  • FileUtils
  • BufferedOutputStream
  • BufferedInputStream
  • OutputStream
  • HttpURLConnection
origin: apache/flink

public static String getFromHTTP(String url, Time timeout) throws Exception {
  final URL u = new URL(url);
  LOG.info("Accessing URL " + url + " as URL: " + u);
  final long deadline = timeout.toMilliseconds() + System.currentTimeMillis();
  while (System.currentTimeMillis() <= deadline) {
    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
    connection.setConnectTimeout(100000);
    connection.connect();
    if (Objects.equals(HttpResponseStatus.SERVICE_UNAVAILABLE, HttpResponseStatus.valueOf(connection.getResponseCode()))) {
      // service not available --> Sleep and retry
      LOG.debug("Web service currently not available. Retrying the request in a bit.");
      Thread.sleep(100L);
    } else {
      InputStream is;
      if (connection.getResponseCode() >= 400) {
        // error!
        LOG.warn("HTTP Response code when connecting to {} was {}", url, connection.getResponseCode());
        is = connection.getErrorStream();
      } else {
        is = connection.getInputStream();
      }
      return IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET);
    }
  }
  throw new TimeoutException("Could not get HTTP response in time since the service is still unavailable.");
}
origin: k9mail/k-9

private static void copyFile(File from, File to) throws IOException {
  FileInputStream in = new FileInputStream(from);
  FileOutputStream out = new FileOutputStream(to);
  try {
    byte[] buffer = new byte[1024];
    int count;
    while ((count = in.read(buffer)) > 0) {
      out.write(buffer, 0, count);
    }
    out.close();
  } finally {
    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
  }
}
origin: iBotPeaches/Apktool

  public static File extractToTmp(String resourcePath, String tmpPrefix, Class clazz) throws BrutException {
    try {
      InputStream in = clazz.getResourceAsStream(resourcePath);
      if (in == null) {
        throw new FileNotFoundException(resourcePath);
      }
      File fileOut = File.createTempFile(tmpPrefix, null);
      fileOut.deleteOnExit();
      OutputStream out = new FileOutputStream(fileOut);
      IOUtils.copy(in, out);
      in.close();
      out.close();
      return fileOut;
    } catch (IOException ex) {
      throw new BrutException("Could not extract resource: " + resourcePath, ex);
    }
  }
}
origin: commons-io/commons-io

/**
 * Gets the contents of a <code>URL</code> as a <code>byte[]</code>.
 *
 * @param url the <code>URL</code> to read
 * @return the requested byte array
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O exception occurs
 * @since 2.4
 */
public static byte[] toByteArray(final URL url) throws IOException {
  final URLConnection conn = url.openConnection();
  try {
    return IOUtils.toByteArray(conn);
  } finally {
    close(conn);
  }
}
origin: gocd/gocd

public void streamToFile(InputStream stream, File dest) throws IOException {
  dest.getParentFile().mkdirs();
  FileOutputStream out = new FileOutputStream(dest, true);
  try {
    IOUtils.copyLarge(stream, out);
  } finally {
    IOUtils.closeQuietly(out);
  }
}
origin: gocd/gocd

  output = new FileOutputStream(file);
  IOUtils.copy(input, output);
} catch (Exception e) {
  return handleExceptionDuringFileHandling(validation, e);
} finally {
  try {
    input.close();
    if (output != null) {
      output.flush();
      output.close();
origin: syncany/syncany

/**
 * Downloads the plugin JAR from the given URL to a temporary
 * local location.
 */
private File downloadPluginJar(String pluginJarUrl) throws Exception {
  URL pluginJarFile = new URL(pluginJarUrl);
  logger.log(Level.INFO, "Querying " + pluginJarFile + " ...");
  URLConnection urlConnection = pluginJarFile.openConnection();
  urlConnection.setConnectTimeout(2000);
  urlConnection.setReadTimeout(2000);
  File tempPluginFile = File.createTempFile("syncany-plugin", "tmp");
  tempPluginFile.deleteOnExit();
  logger.log(Level.INFO, "Downloading to " + tempPluginFile + " ...");
  FileOutputStream tempPluginFileOutputStream = new FileOutputStream(tempPluginFile);
  InputStream remoteJarFileInputStream = urlConnection.getInputStream();
  IOUtils.copy(remoteJarFileInputStream, tempPluginFileOutputStream);
  remoteJarFileInputStream.close();
  tempPluginFileOutputStream.close();
  if (!tempPluginFile.exists() || tempPluginFile.length() == 0) {
    throw new Exception("Downloading plugin file failed, URL was " + pluginJarUrl);
  }
  return tempPluginFile;
}
origin: k9mail/k-9

private void copyAttachmentFromFile(String resourceName, int attachmentId, int expectedFilesize) throws IOException {
  File resourceFile = new File(getClass().getResource("/attach/" + resourceName).getFile());
  File attachmentFile = new File(attachmentDir, Integer.toString(attachmentId));
  BufferedInputStream input = new BufferedInputStream(new FileInputStream(resourceFile));
  BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(attachmentFile));
  int copied = IOUtils.copy(input, output);
  input.close();
  output.close();
  Assert.assertEquals(expectedFilesize, copied);
}
origin: alibaba/jstorm

public static void getYarnConfFromJar(String jarPath) {
  String confPath = jarPath + JOYConstants.CONF_NAME;
  try {
    InputStream stream = new FileInputStream(confPath);
    FileOutputStream out = new FileOutputStream(JOYConstants.CONF_NAME);
    byte[] data = IOUtils.toByteArray(stream);
    out.write(data);
    out.close();
  } catch (Exception e) {
    throw new IllegalArgumentException(
        "No configuration file specified to be executed by application master to launch process");
  }
}
origin: BroadleafCommerce/BroadleafCommerce

protected void cacheStaticCompressedFileInFileSystem(HttpServletRequest request, HttpServletResponse response, FilterChain chain, File targetFile) throws IOException, ServletException {
  String tempRoot = UUID.randomUUID().toString();
  File tempFile = File.createTempFile(tempRoot, ".tmp");
  FileSystemResponseWrapper wrapper = new FileSystemResponseWrapper(response, tempFile);
  chain.doFilter(request, wrapper);
  wrapper.closeFileOutputStream();
  File compressedFile = File.createTempFile(tempRoot, ".tmpgz");
  OutputStream compressedOut = new GZIPOutputStream(new FileOutputStream(compressedFile));
  StreamUtils.copy(new BufferedInputStream(new FileInputStream(tempFile)), compressedOut);
  IOUtils.closeQuietly(compressedOut);
  tempFile.delete();
  atomicMove.replaceExisting(compressedFile, targetFile);
}
origin: commons-io/commons-io

@Test public void testReadLines_InputStream() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  InputStream in = null;
  try {
    final String[] data = new String[] { "hello", "world", "", "this is", "some text" };
    TestUtils.createLineBasedFile(file, data);
    in = new FileInputStream(file);
    final List<String> lines = IOUtils.readLines(in);
    assertEquals(Arrays.asList(data), lines);
    assertEquals(-1, in.read());
  } finally {
    IOUtils.closeQuietly(in);
    TestUtils.deleteFile(file);
  }
}
origin: SonarSource/sonarqube

/**
 * Returns the message contained in {@code file}. Throws an unchecked exception
 * if the file does not exist, is empty or does not contain message with the
 * expected type.
 */
public static <MSG extends Message> MSG read(File file, Parser<MSG> parser) {
 InputStream input = null;
 try {
  input = new BufferedInputStream(new FileInputStream(file));
  return parser.parseFrom(input);
 } catch (Exception e) {
  throw ContextException.of("Unable to read message", e).addContext("file", file);
 } finally {
  IOUtils.closeQuietly(input);
 }
}
origin: apache/incubator-pinot

 private void createInvalidTarFile(File nonDirFile, File tarGzPath) {
  try (FileOutputStream fOut = new FileOutputStream(new File(tarGzPath.getPath()));
    BufferedOutputStream bOut = new BufferedOutputStream(fOut);
    GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(bOut);
    TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut)) {
   tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

   // Mock the file that doesn't use the correct file name.
   String badEntryName = "../foo/bar";
   TarArchiveEntry tarEntry = new TarArchiveEntry(nonDirFile, badEntryName);
   tOut.putArchiveEntry(tarEntry);
   IOUtils.copy(new FileInputStream(nonDirFile), tOut);
   tOut.closeArchiveEntry();
  } catch (IOException e) {
   Assert.fail("Unexpected Exception!!");
  }
 }
}
origin: gocd/gocd

private void extractTo(ZipEntry entry, InputStream entryInputStream, File toDir) throws IOException {
  String entryName = nonRootedEntryName(entry);
  File outputFile = new File(toDir, entryName);
  if (isDirectory(entryName)) {
    outputFile.mkdirs();
    return;
  }
  FileOutputStream os = null;
  try {
    os = new FileOutputStream(outputFile);
    int bytes;
    while ((bytes = entryInputStream.read(fileBuffer)) > 0) {
      os.write(fileBuffer, 0, bytes);
    }
  } catch (IOException e) {
    throw e;
  } finally {
    IOUtils.closeQuietly(os);
  }
}
origin: commons-io/commons-io

dir.mkdirs();
final OutputStream tarFileAStream = FileUtils.openOutputStream(tarFileA);
TestUtils.generateTestData(tarFileAStream, tarMagicNumberOffset);
IOUtils.write(tarMagicNumber, tarFileAStream, StandardCharsets.UTF_8);
tarFileAStream.close();
    new BufferedOutputStream(new FileOutputStream(randomFileB))) {
  TestUtils.generateTestData(output, 2 * tarMagicNumberOffset);
origin: commons-io/commons-io

@Test public void testCopy_ByteArray_OutputStream() throws Exception {
  final File destination = TestUtils.newFile(getTestDirectory(), "copy8.txt");
  byte[] in;
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
    in = IOUtils.toByteArray(fin);
  }
  try (FileOutputStream fout = new FileOutputStream(destination)) {
    CopyUtils.copy(in, fout);
    fout.flush();
    TestUtils.checkFile(destination, m_testFile);
    TestUtils.checkWrite(fout);
  }
  TestUtils.deleteFile(destination);
}
origin: syncany/syncany

private Map<File, File> reassembleFiles(Map<File, List<ChunkChecksum>> inputFileToChunkIDs, Map<ChunkChecksum, File> extractedChunkIDToChunkFile)
    throws IOException {
  Map<File, File> inputFileToOutputFile = new HashMap<File, File>();
  for (Map.Entry<File, List<ChunkChecksum>> inputFileToChunkIDsEntry : inputFileToChunkIDs.entrySet()) {
    File inputFile = inputFileToChunkIDsEntry.getKey();
    List<ChunkChecksum> chunkIDs = inputFileToChunkIDsEntry.getValue();
    File outputFile = new File(tempDir + "/reassembledfile-" + inputFile.getName());
    FileOutputStream outputFileOutputStream = new FileOutputStream(outputFile);
    logger.log(Level.INFO, "- Reassemble file " + inputFile + " to " + outputFile + " ...");
    for (ChunkChecksum chunkID : chunkIDs) {
      File extractedChunkFile = extractedChunkIDToChunkFile.get(chunkID);
      logger.log(Level.INFO, "  + Appending " + chunkID + " (file: " + extractedChunkFile + ") to " + outputFile + " ...");
      IOUtils.copy(new FileInputStream(extractedChunkFile), outputFileOutputStream);
    }
    inputFileToOutputFile.put(inputFile, outputFile);
  }
  return inputFileToOutputFile;
}
origin: apache/hive

private String getURLResponseAsString(String baseURL) throws IOException {
 URL url = new URL(baseURL);
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
 StringWriter writer = new StringWriter();
 IOUtils.copy(conn.getInputStream(), writer, "UTF-8");
 return writer.toString();
}
origin: gocd/gocd

@Override
protected void handleFile(File file, int depth, Collection results) throws IOException {
  if (excludeFiles.contains(file.getAbsolutePath())) {
    return;
  }
  zipStream.putNextEntry(new ZipEntry(fromRoot(file)));
  try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
    IOUtils.copy(in, zipStream);
  }
}
origin: iBotPeaches/Apktool

public static boolean assembleSmaliFile(InputStream is,DexBuilder dexBuilder, boolean verboseErrors,
                    boolean printTokens, File smaliFile) throws IOException, RecognitionException {
  // copy our filestream into a tmp file, so we don't overwrite
  File tmp = File.createTempFile("BRUT",".bak");
  tmp.deleteOnExit();
  OutputStream os = new FileOutputStream(tmp);
  IOUtils.copy(is, os);
  os.close();
  return assembleSmaliFile(tmp,dexBuilder, verboseErrors, printTokens);
}
org.apache.commons.ioIOUtils

Javadoc

General IO stream manipulation utilities.

This class provides static utility methods for input/output operations.

  • closeQuietly - these methods close a stream ignoring nulls and exceptions
  • toXxx/read - these methods read data from a stream
  • write - these methods write data to a stream
  • copy - these methods copy all the data from one stream to another
  • contentEquals - these methods compare the content of two streams

The byte-to-char methods and char-to-byte methods involve a conversion step. Two methods are provided in each case, one that uses the platform default encoding and the other which allows you to specify an encoding. You are encouraged to always specify an encoding because relying on the platform default can lead to unexpected results, for example when moving from development to production.

All the methods in this class that read a stream are buffered internally. This means that there is no cause to use a BufferedInputStream or BufferedReader. The default buffer size of 4K has been shown to be efficient in tests.

Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.

Origin of code: Excalibur.

Most used methods

  • toString
    Get the contents of a byte[] as a String using the specified character encoding. Character encoding
  • closeQuietly
    Closes a Closeable unconditionally. Equivalent to Closeable#close(), except any exceptions will be i
  • copy
    Copy chars from a Reader to a Writer. This method buffers the input internally, so there is no need
  • toByteArray
    Gets the contents of a URLConnection as a byte[].
  • readLines
    Get the contents of a Reader as a list of Strings, one entry per line. This method buffers the input
  • write
    Writes chars from a char[] to a Writer using the default character encoding of the platform.
  • toInputStream
    Converts the specified string to an input stream, encoded as bytes using the specified character enc
  • copyLarge
    Copies chars from a large (over 2GB) Reader to a Writer. This method uses the provided buffer, so th
  • lineIterator
    Return an Iterator for the lines in a Reader.LineIterator holds a reference to the openReader specif
  • readFully
    Reads the requested number of bytes or fail if there are not enough left. This allows for the possib
  • contentEquals
    Compare the contents of two Readers to determine if they are equal or not. This method buffers the i
  • read
    Reads bytes from a ReadableByteChannel. This implementation guarantees that it will read as many byt
  • contentEquals,
  • read,
  • writeLines,
  • toCharArray,
  • skip,
  • skipFully,
  • close,
  • toBufferedReader,
  • toBufferedInputStream,
  • contentEqualsIgnoreEOL

Popular in Java

  • Finding current android device location
  • scheduleAtFixedRate (ScheduledExecutorService)
  • getSupportFragmentManager (FragmentActivity)
  • scheduleAtFixedRate (Timer)
  • Pointer (com.sun.jna)
    An abstraction for a native pointer data type. A Pointer instance represents, on the Java side, a na
  • FileNotFoundException (java.io)
    Thrown when a file specified by a program cannot be found.
  • Notification (javax.management)
  • SSLHandshakeException (javax.net.ssl)
    The exception that is thrown when a handshake could not be completed successfully.
  • XPath (javax.xml.xpath)
    XPath provides access to the XPath evaluation environment and expressions. Evaluation of XPath Expr
  • Option (scala)
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now