Example usage for java.nio.channels Channels newChannel

List of usage examples for java.nio.channels Channels newChannel

Introduction

In this page you can find the example usage for java.nio.channels Channels newChannel.

Prototype

public static WritableByteChannel newChannel(OutputStream out) 

Source Link

Document

Constructs a channel that writes bytes to the given stream.

Usage

From source file:info.fetter.rrdclient.FetchCommand.java

@Override
public void execute(OutputStream out) {
    String command = "fetch " + fileName + " " + consolidationFunction;
    for (String arg : args) {
        command += " " + arg;
    }/*from  w  w w . j  av a2 s .c o  m*/
    try {
        ByteBuffer response = sendCommandToServer(command);
        WritableByteChannel channel = Channels.newChannel(out);
        channel.write(response);
        isOutputParsed = false;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:uk.codingbadgers.bootstrap.download.EtagDownload.java

public void download() {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet assetRequest = new HttpGet(remote);
    assetRequest.setHeader(new BasicHeader("Accept", BootstrapConstants.ASSET_MIME_TYPE));

    if (local.exists()) {
        assetRequest/*  w  w  w. ja va2 s  .co m*/
                .setHeader(new BasicHeader("If-None-Match", "\"" + ChecksumGenerator.createMD5(local) + "\""));
    }

    try {
        HttpResponse response = client.execute(assetRequest);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
            // No need to download, its already latest
            System.out.println("File " + local.getName() + " is up to date with remote, no need to download");
        } else if (status.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY
                || status.getStatusCode() == HttpStatus.SC_OK) {
            // Update local version
            System.err.println("Downloading " + local.getName());

            HttpEntity entity = response.getEntity();

            ReadableByteChannel rbc = Channels.newChannel(entity.getContent());
            FileOutputStream fos = new FileOutputStream(local);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

            EntityUtils.consume(entity);

            String hash = "\"" + ChecksumGenerator.createMD5(local) + "\"";
            if (!hash.equalsIgnoreCase(response.getFirstHeader("Etag").getValue())) {
                throw new BootstrapException(
                        "Error downloading file (" + local.getName() + ")\n[expected hash: "
                                + response.getFirstHeader("Etag").getValue() + " but got " + hash + "]");
            }

            System.out.println("Downloaded " + local.getName());

        } else {
            throw new BootstrapException("Error getting update from github. Error: " + status.getStatusCode()
                    + status.getReasonPhrase());
        }
    } catch (IOException ex) {
        throw new BootstrapException(ex);
    } finally {
        close(client);
    }
}

From source file:com.github.neoio.net.message.staging.memory.MemoryMessageStaging.java

@Override
public int writeTempWriteBytes(ByteBuffer buffer) throws NetIOException {
    return NIOUtils.writeToChannel(Channels.newChannel(writeTempStageCount), buffer);
}

From source file:org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload.java

/**
 * Assign this payload to the given {@link HttpOutputMessage}.
 * @param message the message to assign this payload to
 * @throws IOException in case of I/O errors
 *//*from w  w  w .  j ava 2  s .  co m*/
public void assignTo(HttpOutputMessage message) throws IOException {
    Assert.notNull(message, "Message must not be null");
    HttpHeaders headers = message.getHeaders();
    headers.setContentLength(this.data.remaining());
    headers.add(SEQ_HEADER, Long.toString(getSequence()));
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    WritableByteChannel body = Channels.newChannel(message.getBody());
    while (this.data.hasRemaining()) {
        body.write(this.data);
    }
    body.close();
}

From source file:org.jboss.qa.jcontainer.util.executor.ProcessExecutor.java

public Process asyncExecute() throws IOException {
    if (processBuilder == null) {
        processBuilder = new ProcessBuilder(commands);
    }//from  ww  w .  j av  a  2s.  c  om

    if (outputStream == null) {
        if (SystemUtils.IS_OS_HP_UX) {
            outputStream = System.out;
        } else {
            processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        }
    }
    if (errorStream == null && !redirectError) {
        if (SystemUtils.IS_OS_HP_UX) {
            outputStream = System.err;
        } else {
            processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
        }
    }
    processBuilder.redirectErrorStream(redirectError);

    final Process process = processBuilder.start();

    final ExecutorService executeService = Executors.newCachedThreadPool();
    final List<Future> futures = new ArrayList<>();

    if (outputStream != null) {
        final Pipe pipe = Pipe.open();
        futures.add(executeService
                .submit(new CopyIntoChannel(Channels.newChannel(process.getInputStream()), pipe.sink())));
        futures.add(
                executeService.submit(new CopyIntoChannel(pipe.source(), Channels.newChannel(outputStream))));
    }
    if (errorStream != null && !redirectError) {
        final Pipe pipe = Pipe.open();
        futures.add(executeService
                .submit(new CopyIntoChannel(Channels.newChannel(process.getErrorStream()), pipe.sink())));
        futures.add(
                executeService.submit(new CopyIntoChannel(pipe.source(), Channels.newChannel(errorStream))));
    }

    final Future<Integer> future = executeService.submit(new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {
            process.waitFor();
            for (Future f : futures) {
                f.get();
            }
            return process.exitValue();
        }
    });

    final Process proxyProcess = new ProcessWrapper(process, future);

    executeService.shutdown();
    return proxyProcess;
}

From source file:gobblin.data.management.copy.writer.TarArchiveInputStreamDataWriter.java

/**
 * Untars the passed in {@link FileAwareInputStream} to the task's staging directory. Uses the name of the root
 * {@link TarArchiveEntry} in the stream as the directory name for the untarred file. The method also commits the data
 * by moving the file from staging to output directory.
 *
 * @see gobblin.data.management.copy.writer.FileAwareInputStreamDataWriter#write(gobblin.data.management.copy.FileAwareInputStream)
 *///w  ww .  ja  v a2 s  .c  om
@Override
public void writeImpl(InputStream inputStream, Path writeAt, CopyableFile copyableFile) throws IOException {
    this.closer.register(inputStream);

    TarArchiveInputStream tarIn = new TarArchiveInputStream(inputStream);
    final ReadableByteChannel inputChannel = Channels.newChannel(tarIn);
    TarArchiveEntry tarEntry;

    // flush the first entry in the tar, which is just the root directory
    tarEntry = tarIn.getNextTarEntry();
    String tarEntryRootName = StringUtils.remove(tarEntry.getName(), Path.SEPARATOR);

    log.info("Unarchiving at " + writeAt);

    try {
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {

            // the API tarEntry.getName() is misleading, it is actually the path of the tarEntry in the tar file
            String newTarEntryPath = tarEntry.getName().replace(tarEntryRootName, writeAt.getName());
            Path tarEntryStagingPath = new Path(writeAt.getParent(), newTarEntryPath);

            if (tarEntry.isDirectory() && !this.fs.exists(tarEntryStagingPath)) {
                this.fs.mkdirs(tarEntryStagingPath);
            } else if (!tarEntry.isDirectory()) {
                FSDataOutputStream out = this.fs.create(tarEntryStagingPath, true);
                final WritableByteChannel outputChannel = Channels.newChannel(out);
                try {
                    StreamCopier copier = new StreamCopier(inputChannel, outputChannel);
                    if (isInstrumentationEnabled()) {
                        copier.withCopySpeedMeter(this.copySpeedMeter);
                    }
                    this.bytesWritten.addAndGet(copier.copy());
                    if (isInstrumentationEnabled()) {
                        log.info("File {}: copied {} bytes, average rate: {} B/s",
                                copyableFile.getOrigin().getPath(), this.copySpeedMeter.getCount(),
                                this.copySpeedMeter.getMeanRate());
                    } else {
                        log.info("File {} copied.", copyableFile.getOrigin().getPath());
                    }
                } finally {
                    out.close();
                    outputChannel.close();
                }
            }
        }
    } finally {
        tarIn.close();
        inputChannel.close();
        inputStream.close();
    }
}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

public void write(OutputStream output) throws IOException {

    baseBuffer.rewind();/*from   ww w .jav a  2  s.c om*/
    checkBuffer.rewind();
    tailBuffer.rewind();

    int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity());
    int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity());

    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));

    dataOutput.writeBoolean(compact);
    dataOutput.writeInt(baseCheckSize);
    dataOutput.writeInt(tailSize);

    WritableByteChannel channel = Channels.newChannel(dataOutput);

    ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    tmpBuffer = ByteBuffer.allocate(tailSize * 2);
    CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer();
    tmpCharBuffer.put(tailBuffer.array(), 0, tailSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);

    dataOutput.flush();
}

From source file:com.alibaba.otter.shared.common.utils.NioUtils.java

/**
 * ??copy//from  ww  w  .j  a  v a2  s. c o  m
 */
public static long copy(InputStream input, OutputStream output, long offset, long count) throws IOException {
    long rcount = 0;
    long n = 0;
    if (input instanceof FileInputStream) {
        FileChannel inChannel = ((FileInputStream) input).getChannel();
        WritableByteChannel outChannel = Channels.newChannel(output);
        rcount = inChannel.transferTo(offset, count, outChannel);
    } else if (output instanceof FileOutputStream) {
        FileChannel outChannel = ((FileOutputStream) output).getChannel();
        ReadableByteChannel inChannel = Channels.newChannel(input);
        do {
            if (count < DEFAULT_BUFFER_SIZE) {
                n = outChannel.transferFrom(inChannel, offset + rcount, count);
            } else {
                n = outChannel.transferFrom(inChannel, offset + rcount, DEFAULT_BUFFER_SIZE);
            }
            count -= n;
            rcount += n;
        } while (n > 0);
    } else {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        input.skip(offset);
        while (count > 0) {
            if (count < DEFAULT_BUFFER_SIZE) {
                n = input.read(buffer, 0, (int) count);
            } else {
                n = input.read(buffer, 0, DEFAULT_BUFFER_SIZE);
            }

            output.write(buffer, 0, (int) n);
            count -= n;
            rcount += n;
        }
        // ReadableByteChannel inChannel = Channels.newChannel(input);
        // WritableByteChannel outChannel = Channels.newChannel(output);
        //            
        // //ByteBuffer buffer = new ByteBuffer(DEFAULT_BUFFER_SIZE);
        // ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
        // while (-1 != (n = inChannel.read(buffer))) {
        // outChannel.write(buffer);
        // count += n;
        // }
    }
    return rcount;
}

From source file:net.technicpack.launchercore.util.Download.java

@Override
@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;//from   ww w .j a va 2s.  c  om
    try {
        HttpURLConnection conn = Utils.openHttpConnection(url);
        int response = conn.getResponseCode();
        int responseFamily = response / 100;

        if (responseFamily == 3) {
            throw new DownloadException(
                    "The server issued a redirect response which Technic failed to follow.");
        } else if (responseFamily != 2) {
            throw new DownloadException("The server issued a " + response + " response code.");
        }

        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

From source file:com.quanticate.opensource.compressingcontentstore.CompressingContentWriter.java

@Override
protected WritableByteChannel getDirectWritableChannel() throws ContentIOException {
    // Get the raw writer onto the real stream
    OutputStream rawOut = realContentWriter.getContentOutputStream();

    try {/*from  w w w . j ava2  s . c  om*/
        // Wrap that with the requested compression
        CompressorOutputStream compOut = new CompressorStreamFactory()
                .createCompressorOutputStream(compressionType, rawOut);

        logger.info("Compressing " + realContentWriter.getContentUrl() + " with " + compressionType);

        // Turn that into a channel and return
        return Channels.newChannel(compOut);
    } catch (CompressorException e) {
        throw new AlfrescoRuntimeException("Error compressing", e);
    }
}