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."); }
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); } }
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); } } }
/** * 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); } }
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); } }
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();
/** * 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; }
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); }
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"); } }
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); }
@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); } }
/** * 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); } }
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!!"); } } }
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); } }
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);
@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); }
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; }
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(); }
@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); } }
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); }