List of usage examples for java.nio.channels Channels newChannel
public static WritableByteChannel newChannel(OutputStream out)
From source file:byps.BWire.java
/** * Reads a ByteBuffer from an InputStream * Closes the InputStream.//from w ww. j ava 2 s . com * @param is * @return * @throws IOException */ public static ByteBuffer bufferFromStream(InputStream is, Boolean gzip) throws IOException { if (is == null) return null; try { ByteBuffer ibuf = ByteBuffer.allocate(10 * 1000); if (gzip != null) { if (gzip) { is = new GZIPInputStream(is); } } else { if (!is.markSupported()) is = new BufferedInputStream(is, 2); is.mark(2); int magic = is.read() | (is.read() << 8); is.reset(); if (magic == GZIPInputStream.GZIP_MAGIC) { is = new GZIPInputStream(is); } } ReadableByteChannel rch = Channels.newChannel(is); while (rch.read(ibuf) != -1) { if (ibuf.remaining() == 0) { ByteBuffer nbuf = ByteBuffer.allocate(ibuf.capacity() * 2); ibuf.flip(); nbuf.put(ibuf); ibuf = nbuf; } } ibuf.flip(); return ibuf; } finally { is.close(); } }
From source file:heigit.ors.routing.RoutingProfilesUpdater.java
private void downloadFile(String url, File destination) { try {//from ww w .j a va 2s . com URL website = new URL(url); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:StreamUtil.java
/** * Copies the content read from InputStream to OutputStream. Uses the NIO Channels to copy. * @param is The InputStream that is read. * @param os The OutputStream where the data is written. * @throws IOException//w w w . j a v a 2 s .co m */ public static void copy(final InputStream is, final OutputStream os) throws IOException { final ReadableByteChannel inChannel = Channels.newChannel(is); final WritableByteChannel outChannel = Channels.newChannel(os); try { final ByteBuffer buffer = ByteBuffer.allocate(65536); while (true) { int bytesRead = inChannel.read(buffer); if (bytesRead == -1) break; buffer.flip(); while (buffer.hasRemaining()) outChannel.write(buffer); buffer.clear(); } } finally { try { inChannel.close(); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } try { outChannel.close(); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } }
From source file:eu.itesla_project.dymola.DymolaAdaptersMatParamsWriter.java
private void writeMLArrayToPath(Path fPath, List<MLArray> mlarray) throws IOException { try (OutputStream w = Files.newOutputStream(fPath)) { //using Writable channel to make it work both for 'standard' and shrinkwrap based filesystems ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel wbc = Channels.newChannel(baos); new MatFileWriter(wbc, mlarray); w.write(baos.toByteArray());// w w w. j a v a 2 s.co m } }
From source file:edu.usc.pgroup.floe.client.FloeClient.java
/** * Uploads the file to the coordinator./*from w ww . jav a2s . co m*/ * The file is uploaded relative to the coordinator's scratch folder. * * @param fileName name of the file to be stored on the coordinator. * @return the base fileName which may be used for downloading the file * later. */ public final String uploadFileSync(final String fileName) { String baseFile = FilenameUtils.getName(fileName); try { int fid = getClient().beginFileUpload(baseFile); ReadableByteChannel inChannel = Channels.newChannel(new FileInputStream(fileName)); ByteBuffer buffer = ByteBuffer.allocate(Utils.Constants.BUFFER_SIZE); while (inChannel.read(buffer) > 0) { buffer.flip(); getClient().uploadChunk(fid, buffer); buffer.clear(); } inChannel.close(); getClient().finishUpload(fid); } catch (TException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } catch (FileNotFoundException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } catch (IOException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } return baseFile; }
From source file:org.geowebcache.service.kml.KMZHelper.java
/** * Writes two byte[] into a zip/*from w w w . j a v a2s .c o m*/ * -> like a zipfile with two files * * @param namePfx prefix for files inside file * @param overlay * @param data * @param out * @throws IOException */ private static void writeZippedKML(String namePfx, String formatExtension, byte[] overlay, Resource data, OutputStream out) throws IOException { ZipOutputStream zipos = new ZipOutputStream(out); // High compression //zipos.setLevel(9); // Add the overlay, links to the next content ZipEntry zeOl = new ZipEntry("netlinks_" + namePfx + ".kml"); zipos.putNextEntry(zeOl); zipos.write(overlay); // Add the actual data, if applicable if (data != null) { ZipEntry zeData = new ZipEntry("data_" + namePfx + "." + formatExtension); zipos.putNextEntry(zeData); WritableByteChannel outch = Channels.newChannel(zipos); data.transferTo(outch); } zipos.finish(); }
From source file:com.spectralogic.ds3client.commands.GetObjectRequest.java
public GetObjectRequest(final String bucketName, final String objectName, final String job, final long offset, @Nonnull final OutputStream stream) { Preconditions.checkNotNull(stream, "Stream may not be null."); this.bucketName = bucketName; this.objectName = objectName; this.job = job; this.offset = offset; this.channel = Channels.newChannel(stream); this.updateQueryParam("job", job); this.updateQueryParam("offset", offset); }
From source file:com.github.neoio.net.message.staging.memory.MemoryMessageStaging.java
@Override protected ReadableByteChannel getPrimaryStageChannel() { return Channels.newChannel(new ByteArrayInputStream(primaryStage.toByteArray())); }
From source file:com.cloud.maint.UpgradeManagerImpl.java
public String deployNewAgent(String url) { s_logger.info("Updating agent with binary from " + url); final HttpClient client = new HttpClient(s_httpClientManager); final GetMethod method = new GetMethod(url); int response; File file = null;//from w w w . j a va 2 s . c o m try { response = client.executeMethod(method); if (response != HttpURLConnection.HTTP_OK) { s_logger.warn("Retrieving the agent gives response code: " + response); return "Retrieving the file from " + url + " got response code: " + response; } final InputStream is = method.getResponseBodyAsStream(); file = File.createTempFile("agent-", "-" + Long.toString(new Date().getTime())); file.deleteOnExit(); s_logger.debug("Retrieving new agent into " + file.getAbsolutePath()); final FileOutputStream fos = new FileOutputStream(file); final ByteBuffer buffer = ByteBuffer.allocate(2048); final ReadableByteChannel in = Channels.newChannel(is); final WritableByteChannel out = fos.getChannel(); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } in.close(); out.close(); s_logger.debug("New Agent zip file is now retrieved"); } catch (final HttpException e) { return "Unable to retrieve the file from " + url; } catch (final IOException e) { return "Unable to retrieve the file from " + url; } finally { method.releaseConnection(); } file.delete(); return "File will be deployed."; }
From source file:com.hortonworks.amstore.view.AmbariStoreHelper.java
public static void downloadFile(String url, String localFilePath) { try {/*from w ww . ja v a 2s . c o m*/ URL website = new URL(url); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); // unix specific FileOutputStream fos = new FileOutputStream(localFilePath); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); } catch (java.net.MalformedURLException e) { } catch (java.io.FileNotFoundException e) { } catch (java.io.IOException e) { } }