public static long stream(InputStream input, OutputStream output) throws IOException { ReadableByteChannel inputChannel = null; WritableByteChannel outputChannel = null; try { inputChannel = Channels.newChannel(input); outputChannel = Channels.newChannel(output); ByteBuffer buffer = ByteBuffer.allocate(10240); long size = 0; while (inputChannel.read(buffer) != -1) { buffer.flip(); size += outputChannel.write(buffer); buffer.clear(); } return size; } finally { if (outputChannel != null) try { outputChannel.close(); } catch (IOException ignore) { /**/ } if (inputChannel != null) try { inputChannel.close(); } catch (IOException ignore) { /**/ } } }
public static void main(String[] args) throws IOException { Movie video = MovieCreator.build( Channels.newChannel( Mp4WithAudioDelayExample.class.getResourceAsStream("/count-video.mp4"))); Movie audio = MovieCreator.build( Channels.newChannel( Mp4WithAudioDelayExample.class.getResourceAsStream("/count-english-audio.mp4"))); List<Track> videoTracks = video.getTracks(); video.setTracks(new LinkedList<Track>()); List<Track> audioTracks = audio.getTracks(); for (Track videoTrack : videoTracks) { video.addTrack(new AppendTrack(videoTrack, videoTrack)); } for (Track audioTrack : audioTracks) { audioTrack.getTrackMetaData().setStartTime(10.0); video.addTrack(audioTrack); } IsoFile out = new DefaultMp4Builder().build(video); FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4"))); out.getBox(fos.getChannel()); fos.close(); }
public String copy(String basePath, ImageRef source, String targetDirName) { try { ByteBuffer buffer = ByteBuffer.allocate(16 * 1024); Resource res = source.eResource(); URI uri = source.eResource().getURI(); URI inPath = URI.createURI(source.getPath()).resolve(uri); URI outPath = URI.createURI(source.getPath()) .resolve(URI.createFileURI(new File(targetDirName).getAbsolutePath())); ReadableByteChannel inChannel = Channels.newChannel(res.getResourceSet().getURIConverter().createInputStream(inPath)); WritableByteChannel outChannel = Channels.newChannel(res.getResourceSet().getURIConverter().createOutputStream(outPath)); while (inChannel.read(buffer) != -1) { buffer.flip(); outChannel.write(buffer); buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { outChannel.write(buffer); } outChannel.close(); return outPath.toFileString(); } catch (IOException e) { // TODO Auto-generated catch block logger.error(e); return null; } }
public static void patchFile(File originalFile, File deltaFile, File patchedFile) throws PortalException { FileInputStream originalFileInputStream = null; FileChannel originalFileChannel = null; FileOutputStream patchedFileOutputStream = null; WritableByteChannel patchedWritableByteChannel = null; ReadableByteChannel deltaReadableByteChannel = null; try { originalFileInputStream = new FileInputStream(originalFile); originalFileChannel = originalFileInputStream.getChannel(); patchedFileOutputStream = new FileOutputStream(patchedFile); patchedWritableByteChannel = Channels.newChannel(patchedFileOutputStream); FileInputStream deltaInputStream = new FileInputStream(deltaFile); deltaReadableByteChannel = Channels.newChannel(deltaInputStream); ByteChannelReader deltaByteChannelReader = new ByteChannelReader(deltaReadableByteChannel); DeltaUtil.patch(originalFileChannel, patchedWritableByteChannel, deltaByteChannelReader); } catch (Exception e) { throw new PortalException(e); } finally { StreamUtil.cleanUp(originalFileInputStream); StreamUtil.cleanUp(originalFileChannel); StreamUtil.cleanUp(patchedFileOutputStream); StreamUtil.cleanUp(patchedWritableByteChannel); StreamUtil.cleanUp(deltaReadableByteChannel); } }
public static void main(String[] argv) throws IOException { ReadableByteChannel source = Channels.newChannel(System.in); WritableByteChannel dest = Channels.newChannel(System.out); channelCopy2(source, dest); source.close(); dest.close(); }
/** Call to initialize reading and writting network streams */ private void initialize_channels_and_streams() throws IOException { // initialze the channels for reading and writting read_stream_ = socket_.getInputStream(); write_stream_ = socket_.getOutputStream(); read_channel_ = Channels.newChannel(read_stream_); write_channel_ = Channels.newChannel(write_stream_); }
private int runTestsExternal( final CommandRunnerParams params, Build build, Iterable<String> command, Iterable<TestRule> testRules) throws InterruptedException, IOException { TestRunningOptions options = getTestRunningOptions(params); // Walk the test rules, collecting all the specs. List<ExternalTestRunnerTestSpec> specs = Lists.newArrayList(); for (TestRule testRule : testRules) { if (!(testRule instanceof ExternalTestRunnerRule)) { params .getBuckEventBus() .post( ConsoleEvent.severe( String.format( "Test %s does not support external test running", testRule.getBuildTarget()))); return 1; } ExternalTestRunnerRule rule = (ExternalTestRunnerRule) testRule; specs.add(rule.getExternalTestRunnerSpec(build.getExecutionContext(), options)); } // Serialize the specs to a file to pass into the test runner. Path infoFile = params .getCell() .getFilesystem() .resolve(BuckConstant.SCRATCH_PATH.resolve("external_runner_specs.json")); Files.createDirectories(infoFile.getParent()); Files.deleteIfExists(infoFile); params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(infoFile.toFile(), specs); // Launch and run the external test runner, forwarding it's stdout/stderr to the console. // We wait for it to complete then returns its error code. ListeningProcessExecutor processExecutor = new ListeningProcessExecutor(); ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder() .addAllCommand(command) .addAllCommand(withDashArguments) .addCommand("--buck-test-info", infoFile.toString()) .setDirectory(params.getCell().getFilesystem().getRootPath().toFile()) .build(); ForwardingProcessListener processListener = new ForwardingProcessListener( Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr())); ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener); try { return processExecutor.waitForProcess(process, Long.MAX_VALUE, TimeUnit.DAYS); } finally { processExecutor.destroyProcess(process, /* force */ false); processExecutor.waitForProcess(process, Long.MAX_VALUE, TimeUnit.DAYS); } }
private static File writeToTempFile(final InputStream resourceStream) throws IOException, FileNotFoundException { final File tempFile = File.createTempFile("SchemaCrawler", ".dat"); tempFile.deleteOnExit(); try (final OutputStream tempFileStream = new FileOutputStream(tempFile); ) { fastChannelCopy(Channels.newChannel(resourceStream), Channels.newChannel(tempFileStream)); } return tempFile; }
public static void writeFile(File file, byte[] bytes) throws IOException { java.io.FileOutputStream f = new java.io.FileOutputStream(file); final WritableByteChannel outputChannel = Channels.newChannel(f); final ReadableByteChannel inputChannel = Channels.newChannel(new ByteArrayInputStream(bytes)); try { fastChannelCopy(inputChannel, outputChannel); // closing the channels } finally { inputChannel.close(); outputChannel.close(); } }
public static File convertRpmToZip(File file) throws Exception { LOG.info("File: " + file.getAbsolutePath()); FileInputStream fis = new FileInputStream(file); ReadableChannelWrapper in = new ReadableChannelWrapper(Channels.newChannel(fis)); InputStream uncompressed = new GZIPInputStream(fis); in = new ReadableChannelWrapper(Channels.newChannel(uncompressed)); String rpmZipName = file.getName(); rpmZipName = StringUtils.replace(rpmZipName, ".", "-"); rpmZipName = rpmZipName + ".zip"; String rpmZipPath = FilenameUtils.getFullPath(file.getAbsolutePath()); File rpmZipOutput = new File(rpmZipPath + File.separator + rpmZipName); LOG.info("Converting RPM: " + file.getName() + " to ZIP: " + rpmZipOutput.getName()); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(rpmZipOutput)); String rpmName = file.getName(); rpmName = StringUtils.replace(rpmName, ".", "-"); CpioHeader header; int total = 0; do { header = new CpioHeader(); total = header.read(in, total); if (header.getFileSize() > 0) { BoundedInputStream bis = new BoundedInputStream(uncompressed, header.getFileSize()); String relPath = FilenameUtils.separatorsToSystem(header.getName()); relPath = StringUtils.removeStart(relPath, "."); relPath = StringUtils.removeStart(relPath, "/"); relPath = rpmName + File.separator + relPath; relPath = StringUtils.replace(relPath, "\\", "/"); ZipEntry zipEntry = new ZipEntry(relPath); zos.putNextEntry(zipEntry); IOUtils.copy(bis, zos); } else { final int skip = header.getFileSize(); if (uncompressed.skip(skip) != skip) throw new RuntimeException("Skip failed."); } total += header.getFileSize(); } while (!header.isLast()); zos.flush(); zos.close(); return rpmZipOutput; }
public static void copyStreams(final InputStream input, final OutputStream output) throws IOException { final ReadableByteChannel inputChannel = Channels.newChannel(input); final WritableByteChannel outputChannel = Channels.newChannel(output); try { // copy the channels fastChannelCopy(inputChannel, outputChannel); // closing the channels } finally { inputChannel.close(); outputChannel.close(); } }
public static void main(String[] args) throws IOException { FileInputStream fin = new FileInputStream(args[0]); GZIPInputStream gzin = new GZIPInputStream(fin); ReadableByteChannel in = Channels.newChannel(gzin); WritableByteChannel out = Channels.newChannel(System.out); ByteBuffer buffer = ByteBuffer.allocate(65536); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } }
@Test public void run3() throws IOException { final ByteArrayOutputStream expected = new ByteArrayOutputStream(); final ByteArrayOutputStream result = new ByteArrayOutputStream(); for (int k = 0; k < this.chunks; k++) { final byte[] data = new byte[10000 * (k + 1)]; for (int j = 0; j < data.length; j++) { data[j] = (byte) j; } final Deflater def1 = new Deflater(9); final DeflaterOutputStream dos = new DeflaterOutputStream(expected, def1, 2048); dos.write(data); dos.finish(); final Deflater def2 = new Deflater(9); final DeflaterChannelOutput dco = new DeflaterChannelOutput(Channels.newChannel(result), def2, 2048); dco.write(ByteBuffer.wrap(data)); dco.flush(); } Assert.assertArrayEquals(expected.toByteArray(), result.toByteArray()); }
public static boolean downloadOCSSWInstaller() { if (isOcsswInstalScriptDownloadSuccessful()) { return ocsswInstalScriptDownloadSuccessful; } try { URL website = new URL(OCSSW_INSTALLER_URL); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(TMP_OCSSW_INSTALLER); fos.getChannel().transferFrom(rbc, 0, 1 << 24); fos.close(); (new File(TMP_OCSSW_INSTALLER)).setExecutable(true); ocsswInstalScriptDownloadSuccessful = true; } catch (MalformedURLException malformedURLException) { handleException("URL for downloading install_ocssw.py is not correct!"); } catch (FileNotFoundException fileNotFoundException) { handleException( "ocssw installation script failed to download. \n" + "Please check network connection or 'seadas.ocssw.root' variable in the 'seadas.config' file. \n" + "possible cause of error: " + fileNotFoundException.getMessage()); } catch (IOException ioe) { handleException( "ocssw installation script failed to download. \n" + "Please check network connection or 'seadas.ocssw.root' variable in the \"seadas.config\" file. \n" + "possible cause of error: " + ioe.getLocalizedMessage()); } finally { return ocsswInstalScriptDownloadSuccessful; } }
private void writeCompressed(JarOutputStream jarOut, String entryName) throws IOException { File file = entryMap.get(entryName); if ((file.exists()) && (file.isFile())) { JarEntry entry = new JarEntry(entryName); jarOut.putNextEntry(entry); WritableByteChannel outputChannel = null; FileChannel readFromFileChannel = null; FileInputStream inputStream = null; try { outputChannel = Channels.newChannel(jarOut); inputStream = new FileInputStream(file); readFromFileChannel = inputStream.getChannel(); readFromFileChannel.transferTo(0, file.length(), outputChannel); } finally { try { if (jarOut != null) { jarOut.closeEntry(); } if (readFromFileChannel != null) { readFromFileChannel.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { AndmoreLogger.error("Could not close stream: ", e.getMessage()); // $NON-NLS-1$ } } } }
public static void downloadFile(String url, String outputName) throws IOException { URL website = new URL(url); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(outputName); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); }
public void writeTo(Object obj, String mime, OutputStream os) throws IOException { if (obj instanceof InputStream) { InputStream is = (InputStream) obj; byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = is.read(buffer)) != -1) os.write(buffer, 0, len); os.flush(); } else if (obj instanceof FileRegionDataSource) { FileRegionDataSource frds = (FileRegionDataSource) obj; WritableByteChannel wbc = Channels.newChannel(os); wbc.write(frds.getByteBuffer()); os.flush(); } else if (obj instanceof EmptyDataSource) { /* EmptyDataSource eds = (EmptyDataSource)obj; InputStream is = (InputStream) eds.getInputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = is.read(buffer)) != -1) os.write(buffer, 0, len); os.flush(); */ } else if (obj instanceof String) { byte[] bytes = ((String) obj).getBytes(); os.write(bytes); os.flush(); } else { throw new IOException("Unsupported class type - " + obj.getClass().getName()); } }
@Test public void testEncryptDecryptFragmentedMp4() throws Exception { SecretKey sk = new SecretKeySpec(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "AES"); Movie m = MovieCreator.build( CencTracksImplTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/com/googlecode/mp4parser/authoring/samples/1365070268951.mp4"); List<Track> encTracks = new LinkedList<Track>(); for (Track track : m.getTracks()) { encTracks.add(new CencEncryptingTrackImpl(track, UUID.randomUUID(), sk)); } m.setTracks(encTracks); Mp4Builder mp4Builder = new FragmentedMp4Builder(); Container c = mp4Builder.build(m); ByteArrayOutputStream baos = new ByteArrayOutputStream(); c.writeContainer(Channels.newChannel(baos)); c.writeContainer(new FileOutputStream("output.mp4").getChannel()); Movie m2 = MovieCreator.build(new MemoryDataSourceImpl(baos.toByteArray())); List<Track> decTracks = new LinkedList<Track>(); for (Track track : m2.getTracks()) { decTracks.add(new CencDecryptingTrackImpl((CencEncyprtedTrack) track, sk)); } m2.setTracks(decTracks); c = mp4Builder.build(m2); c.writeContainer(new FileOutputStream("output2.mp4").getChannel()); }
/** * transfer Stream * * @param ins * @param targetChannel */ private static void transferStream(InputStream ins, FileChannel targetChannel) { ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10); ReadableByteChannel rbcInst = Channels.newChannel(ins); try { while (-1 != (rbcInst.read(byteBuffer))) { byteBuffer.flip(); targetChannel.write(byteBuffer); byteBuffer.clear(); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (null != rbcInst) { try { rbcInst.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != targetChannel) { try { targetChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/* * (non-Javadoc) * * @see * net.jawr.web.resource.bundle.ResourceBundlesHandler#writeBundleTo(java * .lang.String, java.io.Writer) */ @Override public void writeBundleTo(String bundlePath, Writer writer) throws ResourceNotFoundException { // String text = (String) textCache.get(bundlePath); String text = (String) cacheMgr.get(TEXT_CACHE_PREFIX + bundlePath); try { // If it's not cached yet if (null == text) { String charsetName = rsHandler.getConfig().getResourceCharset().name(); ByteArrayOutputStream baOs = new ByteArrayOutputStream(); WritableByteChannel wrChannel = Channels.newChannel(baOs); Writer tempWriter = Channels.newWriter(wrChannel, charsetName); rsHandler.writeBundleTo(bundlePath, tempWriter); text = baOs.toString(charsetName); cacheMgr.put(TEXT_CACHE_PREFIX + bundlePath, text); } // Write the text to the outputstream writer.write(text); writer.flush(); } catch (IOException e) { throw new BundlingProcessException( "Unexpected IOException writing bundle[" + bundlePath + "]", e); } }
@org.junit.Test public void testMapper() throws Exception { final ArcFileReader reader = new ArcFileReader(); Thread thread = new Thread( new Runnable() { public void run() { try { while (reader.hasMoreItems()) { ArcFileItem item = new ArcFileItem(); reader.getNextItem(item); map(new Text(item.getUri()), item, null, null); } LOG.info("NO MORE ITEMS... BYE"); } catch (IOException e) { LOG.error(StringUtils.stringifyException(e)); } } }); // run the thread ... thread.start(); File file = new File("/Users/rana/Downloads/1213886083018_0.arc.gz"); ReadableByteChannel channel = Channels.newChannel(new FileInputStream(file)); try { int totalBytesRead = 0; for (; ; ) { ByteBuffer buffer = ByteBuffer.allocate(ArcFileReader.DEFAULT_BLOCK_SIZE); int bytesRead = channel.read(buffer); LOG.info("Read " + bytesRead + " From File"); if (bytesRead == -1) { reader.finished(); break; } else { buffer.flip(); totalBytesRead += buffer.remaining(); reader.available(buffer); } } } finally { channel.close(); } // now wait for thread to die ... LOG.info("Done Reading File.... Waiting for ArcFileThread to DIE"); thread.join(); LOG.info("Done Reading File.... ArcFileThread to DIED"); }
/** Caclulates an MD5 hash for each file in the archive. */ public String[] getMD5s() throws FileNotFoundException, NoSuchAlgorithmException, IOException { /** * This could be more efficiently handled during the output phase using a filtering channel, but * would require placeholder values in the archive and some state. This is left for a later * refactoring. */ final ByteBuffer buffer = ByteBuffer.allocate(4096); String[] array = new String[headers.size()]; int x = 0; for (CpioHeader header : headers) { Object object = sources.get(header); String value = ""; if (object instanceof File) { final ReadableChannelWrapper input = new ReadableChannelWrapper(new FileInputStream((File) object).getChannel()); final Key<byte[]> key = input.start("MD5"); while (input.read(buffer) != -1) buffer.rewind(); value = new String(Util.hex(input.finish(key))); input.close(); } else if (object instanceof URL) { final ReadableChannelWrapper input = new ReadableChannelWrapper( Channels.newChannel(((URL) object).openConnection().getInputStream())); final Key<byte[]> key = input.start("MD5"); while (input.read(buffer) != -1) buffer.rewind(); value = new String(Util.hex(input.finish(key))); input.close(); } array[x++] = value; } return array; }
public static void write(HttpServletResponse response, File file) throws IOException { if (response instanceof ByteBufferServletResponse) { ByteBufferServletResponse byteBufferResponse = (ByteBufferServletResponse) response; ByteBuffer byteBuffer = ByteBuffer.wrap(FileUtil.getBytes(file)); byteBufferResponse.setByteBuffer(byteBuffer); } else if (response instanceof StringServletResponse) { StringServletResponse stringResponse = (StringServletResponse) response; String s = FileUtil.read(file); stringResponse.setString(s); } else { FileInputStream fileInputStream = new FileInputStream(file); FileChannel fileChannel = fileInputStream.getChannel(); try { int contentLength = (int) fileChannel.size(); response.setContentLength(contentLength); response.flushBuffer(); fileChannel.transferTo(0, contentLength, Channels.newChannel(response.getOutputStream())); } finally { fileChannel.close(); } } }
private static boolean unsafeDownloadToFile(String url, File output) throws IOException { output.createNewFile(); FileOutputStream fos = new FileOutputStream(output); fos.getChannel().transferFrom(Channels.newChannel(requestStreamHttp(url)), 0, Long.MAX_VALUE); fos.close(); return true; }
private static void saveLog(final String absolutePath, final String downloadUrl) throws Exception { // System.out.println("downloadUrl: " + downloadUrl); final File directory = new File(absolutePath); if (!directory.exists()) { directory.mkdirs(); } final String fileName = FilenameUtils.getName(downloadUrl); final File file = new File(absolutePath + File.separator + fileName); try (FileOutputStream fos = new FileOutputStream(file)) { final URL website = new URL(downloadUrl); final ReadableByteChannel rbc = Channels.newChannel(website.openStream()); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); cleanFile(website.openStream(), file); } catch (MalformedURLException e) { throw e; } catch (IOException e) { throw e; } }
@Before public void setUp() throws Exception { // Download file for test URL website = new URL("http://archive.org/web/images/logo_wayback_210x77.png"); ReadableByteChannel resFile = Channels.newChannel(website.openStream()); // Create file File file = new File(pathTempFile); FileOutputStream fos = new FileOutputStream(pathTempFile); fos.getChannel().transferFrom(resFile, 0, Long.MAX_VALUE); // Save to MultipartFile FileInputStream input = new FileInputStream(file); multipartFile = new MockMultipartFile("file", file.getName(), "image/png", IOUtils.toByteArray(input)); fos.close(); input.close(); // Set paths attachmentService.setStoragePath(storagePath); attachmentService.setPathDefaultPreview(pathDefaultPreview); }
private static Channel prepareStdioChannel(Ruby runtime, STDIO stdio, Object stream) { if (runtime.getPosix().isNative() && stdio.isJVMDefault(stream) && !Platform.IS_WINDOWS) { // use real native channel for stdio return new NativeDeviceChannel(stdio.fileno()); } else { switch (stdio) { case IN: return Channels.newChannel((InputStream) stream); case OUT: case ERR: return Channels.newChannel((OutputStream) stream); default: throw new RuntimeException("invalid stdio: " + stdio); } } }
public static void copy(InputStream in, OutputStream out) throws IOException { ReadableByteChannel source = Channels.newChannel(in); WritableByteChannel target = Channels.newChannel(out); ByteBuffer buffer = ByteBuffer.allocate(16 * 1024); while (source.read(buffer) != -1) { buffer.flip(); // Prepare the buffer to be drained while (buffer.hasRemaining()) { target.write(buffer); } buffer.clear(); // Empty buffer to get ready for filling } source.close(); target.close(); }
/** * Downloads a file from the given {@code url} to the given {@link File} * {@code output}. * * @param url * the URL * @param output * the output file * @throws IOException */ public static void download(String url, File output) throws IOException { URL url1 = new URL(url); ReadableByteChannel rbc = Channels.newChannel(url1.openStream()); FileOutputStream fos = new FileOutputStream(output); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); }
@Override public int write(ByteBuffer src) throws IOException { int remaining = src.remaining(); synchronized (this.written) { Channels.newChannel(this.written).write(src); } return remaining; }