URL website = new URL("http://www.website.com/information.asp"); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream("information.html"); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
public static void copyFile( File from, File to ) throws IOException { if ( !to.exists() ) { to.createNewFile(); } try ( FileChannel in = new FileInputStream( from ).getChannel(); FileChannel out = new FileOutputStream( to ).getChannel() ) { out.transferFrom( in, 0, in.size() ); } }
private boolean validSize() throws Exception { long size = outMetric.getChannel().size(); return size < singleFileSize; }
@Override public void truncate() throws IOException { if ( isOpen.get() ) { throw new IOException( "Cannot truncate while log is open for writing. Close the log then truncate." ); } // Synchronize on isOpen to prevent re-opening while truncating (rare) synchronized ( isOpen ) { File results = new File( resultsFile.get() ); FileChannel channel = new FileOutputStream( results, true ).getChannel(); channel.truncate( 0 ); channel.close(); resultCount.set( 0 ); } }
private File downloadZipFile() throws IOException { // Check if we already have a local copy of the emulator and download it if not. File zipFile = new File(System.getProperty("java.io.tmpdir"), fileName); if (!zipFile.exists() || (md5CheckSum != null && !md5CheckSum.equals(md5(zipFile)))) { if (log.isLoggable(Level.FINE)) { log.fine("Fetching emulator"); } ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openStream()); try (FileOutputStream fos = new FileOutputStream(zipFile)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } } else { if (log.isLoggable(Level.FINE)) { log.fine("Using cached emulator"); } } return zipFile; }
public void run() { try { fileOutputStream = new FileOutputStream( this.fifoName ); fileChannel = fileOutputStream.getChannel(); } catch ( Exception ex ) { this.ex = ex; } }
/** * 拷贝文件流,使用NIO * * @param in 输入 * @param out 输出 * @return 拷贝的字节数 * @throws IORuntimeException IO异常 */ public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException { Assert.notNull(in, "FileInputStream is null!"); Assert.notNull(out, "FileOutputStream is null!"); final FileChannel inChannel = in.getChannel(); final FileChannel outChannel = out.getChannel(); try { return inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw new IORuntimeException(e); } }
public static void copyFile(File source, File dest) throws IOException { try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) { out.transferFrom(in, 0, in.size()); } }
public static void main(String[] args) throws IOException { ClassicMp4ContainerSource classicMp4ContainerSource = null; try { classicMp4ContainerSource = new ClassicMp4ContainerSource(new URI("http://org.mp4parser.s3.amazonaws.com/examples/Cosmos%20Laundromat%20small%20faststart.mp4").toURL().openStream()); } catch (URISyntaxException e) { throw new IOException(e); } List<StreamingTrack> streamingTracks = classicMp4ContainerSource.getTracks(); File f = new File("output.mp4"); FragmentedMp4Writer writer = new FragmentedMp4Writer(streamingTracks, new FileOutputStream(f).getChannel()); System.out.println("Reading and writing started."); classicMp4ContainerSource.call(); writer.close(); System.err.println(f.getAbsolutePath()); }
/** * @param igniteUrl Url to ignite. * @return Ignite file name. */ private String downloadIgnite(String igniteUrl) { try { URL url = new URL(igniteUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); int code = conn.getResponseCode(); if (code == 200) { String fileName = fileName(url.toString()); String filePath = props.igniteLocalWorkDir() + File.separator + fileName; if (new File(filePath).exists()) return fileName; FileOutputStream outFile = new FileOutputStream(filePath); outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE); outFile.close(); return fileName; } else throw new RuntimeException("Got unexpected response code. Response code: " + code); } catch (IOException e) { throw new RuntimeException("Failed update ignite.", e); } }
public FileChannel run() { final String osName = System.getProperty("os.name", "unknown").toLowerCase(Locale.US); try { if (osName.contains("windows")) { return new FileOutputStream("NUL:").getChannel(); } else { return new FileOutputStream("/dev/null").getChannel(); } } catch (FileNotFoundException e) { throw new IOError(e); } } });
/** * Downloads resource to a file, potentially very efficiently. */ public static void downloadFile(String url, File file) throws IOException { InputStream inputStream = new URL(url).openStream(); ReadableByteChannel rbc = Channels.newChannel(inputStream); FileOutputStream fos = new FileOutputStream(file); fos.getChannel().transferFrom(rbc, 0, 1 << 24); } }
/** * 拷贝文件流,使用NIO * * @param in 输入 * @param out 输出 * @return 拷贝的字节数 * @throws IORuntimeException IO异常 */ public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException { Assert.notNull(in, "FileInputStream is null!"); Assert.notNull(out, "FileOutputStream is null!"); final FileChannel inChannel = in.getChannel(); final FileChannel outChannel = out.getChannel(); try { return inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw new IORuntimeException(e); } }
public static void copyFile(File source, File dest) throws IOException { try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) { out.transferFrom(in, 0, in.size()); } }
private File unzipFile(File cachedFile) throws IOException { String filename = cachedFile.getName(); File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); if (!destDir.exists()) { FileOutputStream out = new FileOutputStream(lockFile); try { java.nio.channels.FileLock lock = out.getChannel().lock(); try { // Recheck in case of concurrent processes if (!destDir.exists()) { File tempDir = pluginFiles.createTempDir(); ZipUtils.unzip(cachedFile, tempDir, newLibFilter()); FileUtils.moveDirectory(tempDir, destDir); } } finally { lock.release(); } } finally { out.close(); deleteQuietly(lockFile); } } return destDir; } }
@Override public void download(String downloadUrl, String destination, String userName, String password) throws DownloadException { // force tls to 1.2 since github removed weak cryptographic standards // https://blog.github.com/2018-02-02-weak-cryptographic-standards-removal-notice/ System.setProperty("https.protocols", "TLSv1.2"); String fixedDownloadUrl = downloadUrl; try { fixedDownloadUrl = FilenameUtils.separatorsToUnix(fixedDownloadUrl); URI downloadURI = new URI(fixedDownloadUrl); if ("file".equalsIgnoreCase(downloadURI.getScheme())) { FileUtils.copyFile(new File(downloadURI), new File(destination)); } else { CloseableHttpResponse response = execute(fixedDownloadUrl, userName, password); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode != 200){ throw new DownloadException("Got error code "+ statusCode +" from the server."); } new File(FilenameUtils.getFullPathNoEndSeparator(destination)).mkdirs(); ReadableByteChannel rbc = Channels.newChannel(response.getEntity().getContent()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); } } catch (IOException | URISyntaxException e) { throw new DownloadException("Could not download " + fixedDownloadUrl, e); } }
/** * 快速保存 * * @param path * @param content * @return */ public static boolean saveTxt(String path, String content) { try { FileChannel fc = new FileOutputStream(path).getChannel(); fc.write(ByteBuffer.wrap(content.getBytes())); fc.close(); } catch (Exception e) { logger.throwing("IOUtil", "saveTxt", e); logger.warning("IOUtil saveTxt 到" + path + "失败" + e.toString()); return false; } return true; }
public static void writeToFile(byte[] data, File target) throws IOException { FileOutputStream fo = null; ReadableByteChannel src = null; FileChannel out = null; try { src = Channels.newChannel(new ByteArrayInputStream(data)); fo = new FileOutputStream(target); out = fo.getChannel(); out.transferFrom(src, 0, data.length); } finally { if (fo != null) { fo.close(); } if (src != null) { src.close(); } if (out != null) { out.close(); } } }
public void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.close(); }
public static void main(String[] args) throws IOException { String videoFile = MuxVideoWithAmf0.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/example-sans-amf0.mp4"; Movie video = MovieCreator.build(videoFile); Properties props = new Properties(); props.load(MuxVideoWithAmf0.class.getResourceAsStream("/amf0track.properties")); HashMap<Long, byte[]> samples = new HashMap<Long, byte[]>(); for (String key : props.stringPropertyNames()) { samples.put(Long.parseLong(key), Base64.decodeBase64(props.getProperty(key))); } Track amf0Track = new Amf0Track(samples); amf0Track.getTrackMetaData(); video.addTrack(amf0Track); FragmentedMp4Builder fragmentedMp4Builder = new FragmentedMp4Builder(); fragmentedMp4Builder.setFragmenter(new DefaultFragmenterImpl(2)); Container out = fragmentedMp4Builder.build(video); FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4"))); FileChannel fc = fos.getChannel(); out.writeContainer(fc); fos.close(); } }