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:byps.BWire.java

/**
 * Writes a ByteBuffer to an OutputStream.
 * Closes the OutputStream.//from  www . j a  va 2  s  .co  m
 * @param buf
 * @param os
 * @throws IOException
 */
public static void bufferToStream(ByteBuffer buf, boolean gzip, OutputStream os) throws IOException {
    if (gzip) {
        os = new GZIPOutputStream(os);
    }

    WritableByteChannel wch = null;
    try {
        wch = Channels.newChannel(os);
        wch.write(buf);
    } finally {
        if (wch != null)
            wch.close();
    }
}

From source file:org.basinmc.maven.plugins.minecraft.launcher.DownloadDescriptor.java

/**
 * Fetches the artifact from the server and stores it in a specified file.
 *///from w w w  .  j ava2s . c  om
public void fetch(@Nonnull Path outputFile) throws IOException {
    HttpClient client = HttpClients.createMinimal();

    try {
        HttpGet request = new HttpGet(this.url.toURI());
        HttpResponse response = client.execute(request);

        StatusLine line = response.getStatusLine();

        if (line.getStatusCode() != 200) {
            throw new IOException(
                    "Unexpected status code: " + line.getStatusCode() + " - " + line.getReasonPhrase());
        }

        try (InputStream inputStream = response.getEntity().getContent()) {
            try (FileChannel fileChannel = FileChannel.open(outputFile, StandardOpenOption.CREATE,
                    StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
                try (ReadableByteChannel inputChannel = Channels.newChannel(inputStream)) {
                    fileChannel.transferFrom(inputChannel, 0, Long.MAX_VALUE);
                }
            }
        }
    } catch (URISyntaxException ex) {
        throw new IOException("Received invalid URI from API: " + ex.getMessage(), ex);
    }
}

From source file:org.apache.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 org.apache.gobblin.data.management.copy.writer.FileAwareInputStreamDataWriter#write(org.apache.gobblin.data.management.copy.FileAwareInputStream)
 *//*  w w w .j a v  a 2 s.co m*/
@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 (!FileUtils.isSubPath(writeAt.getParent(), tarEntryStagingPath)) {
                throw new IOException(
                        String.format("Extracted file: %s is trying to write outside of output directory: %s",
                                tarEntryStagingPath, writeAt.getParent()));
            }

            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:org.alfresco.repo.content.cloudstore.S3ContentReader.java

@Override
protected ReadableByteChannel getDirectReadableChannel() throws ContentIOException {
    try {/*from w w  w .j  a va 2  s. co  m*/
        // Confirm the requested object exists
        if (!exists()) {
            throw new ContentIOException("Content object does not exist");
        }
        logger.debug("S3ContentReader Obtaining Input Stream: nodeUrl=" + nodeUrl);
        // Get the object and retrieve the input stream
        S3Object object = s3.getObject(bucket, nodeUrl);
        ReadableByteChannel channel = null;
        InputStream is = object.getDataInputStream();
        channel = Channels.newChannel(is);
        logger.debug("S3ContentReader Success Obtaining Input Stream: nodeUrl=" + nodeUrl);
        return channel;
    } catch (Throwable e) {
        throw new ContentIOException("Failed to open channel: " + this, e);
    }
}

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

@Override
public void download() {
    CloseableHttpClient client = HttpClients.createDefault();
    String hash = null;// ww  w  .j  a  va  2  s.c o  m

    // Get sha1 hash from repo
    try {
        HttpGet request = new HttpGet(remote + ".sha1");
        HttpResponse response = client.execute(request);

        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            hash = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
        }
    } catch (IOException ex) {
        throw new BootstrapException(ex);
    }

    if (local.exists()) {
        String localHash = ChecksumGenerator.createSha1(local);

        if (hash != null && hash.equalsIgnoreCase(localHash)) {
            System.out.println("File " + local.getName() + " is up to date with remote, no need to download");
            return;
        }
    }

    if (!local.getParentFile().exists()) {
        local.getParentFile().mkdirs();
    }

    // Download library from remote
    try {
        HttpGet request = new HttpGet(remote);
        HttpResponse response = client.execute(request);

        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() == HttpStatus.SC_OK) {
            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 localHash = ChecksumGenerator.createSha1(local);
            if (hash != null && !localHash.equalsIgnoreCase(hash)) {
                throw new BootstrapException("Error downloading file (" + local.getName()
                        + ")\n[expected hash: " + localHash + " but got " + hash + "]");
            }

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

        } else {
            throw new BootstrapException("Error download update for " + local.getName() + ", Error: "
                    + status.getStatusCode() + status.getReasonPhrase());
        }
    } catch (IOException ex) {
        throw new BootstrapException(ex);
    }
}

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

/**
 * Return the {@link HttpTunnelPayload} for the given message or {@code null} if there
 * is no payload./*from   w  w  w.  j ava  2  s. c  o  m*/
 * @param message the HTTP message
 * @return the payload or {@code null}
 * @throws IOException in case of I/O errors
 */
public static HttpTunnelPayload get(HttpInputMessage message) throws IOException {
    long length = message.getHeaders().getContentLength();
    if (length <= 0) {
        return null;
    }
    String seqHeader = message.getHeaders().getFirst(SEQ_HEADER);
    Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header");
    ReadableByteChannel body = Channels.newChannel(message.getBody());
    ByteBuffer payload = ByteBuffer.allocate((int) length);
    while (payload.hasRemaining()) {
        body.read(payload);
    }
    body.close();
    payload.flip();
    return new HttpTunnelPayload(Long.valueOf(seqHeader), payload);
}

From source file:nl.jk_5.nailed.plugin.nmm.NmmMappack.java

@Override
public void prepareWorld(File destination, SettableFuture<Void> future) {
    HttpClient httpClient = HttpClientBuilder.create().build();
    try {//w  w  w.ja v  a 2  s.  com
        String mappack = this.path.split("/", 2)[1];
        HttpGet request = new HttpGet("http://nmm.jk-5.nl/" + this.path + "/versions.json");
        HttpResponse response = httpClient.execute(request);
        MappackInfo list = NmmPlugin.gson.fromJson(EntityUtils.toString(response.getEntity(), "UTF-8"),
                MappackInfo.class);

        HttpGet request2 = new HttpGet(
                "http://nmm.jk-5.nl/" + this.path + "/" + mappack + "-" + list.latest + ".zip");
        HttpEntity response2 = httpClient.execute(request2).getEntity();
        if (response2 != null) {
            File mappackZip = new File(destination, "mappack.zip");
            try (ReadableByteChannel source = Channels.newChannel(response2.getContent());
                    FileChannel out = FileChannel.open(mappackZip.toPath(), StandardOpenOption.CREATE_NEW,
                            StandardOpenOption.WRITE)) {
                out.transferFrom(source, 0, Long.MAX_VALUE);
            }
            ZipUtils.extract(mappackZip, destination);
            mappackZip.delete();
            this.dir = destination;
            File dataDir = new File(destination, ".data");
            dataDir.mkdir();
            File metadataLocation = new File(dataDir, "game.xml");
            new File(destination, "game.xml").renameTo(metadataLocation);
            new File(destination, "scripts").renameTo(new File(dataDir, "scripts"));
            File worldsDir = new File(destination, "worlds");
            for (File f : worldsDir.listFiles()) {
                f.renameTo(new File(destination, f.getName()));
            }
            worldsDir.delete();
            //metadata = XmlMappackMetadata.fromFile(metadataLocation);
            future.set(null);
        } else {
            future.setException(new RuntimeException(
                    "Got an empty response while downloading mappack " + this.path + " from nmm.jk-5.nl"));
        }
    } catch (Exception e) {
        future.setException(
                new RuntimeException("Was not able to download mappack " + this.path + " from nmm.jk-5.nl", e));
    }
}

From source file:tilt.image.Picture.java

/**
 * Create a picture. Pictures stores links to the various image files.
 * @param options options from the geoJSON file
 * @param text the text to align with//from  w w  w  . j  a  v  a2 s  .c om
 * @param poster the ipaddress of the poster of the image (DDoS prevention)
 * @throws TiltException 
 */
public Picture(Options options, TextIndex text, InetAddress poster) throws TiltException {
    try {
        URL url = new URL(options.url);
        // use url as id for now
        id = options.url;
        this.text = text;
        this.poster = poster;
        // try to register the picture
        PictureRegistry.register(this, options.url);
        // fetch picture from url
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        orig = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX);
        FileOutputStream fos = new FileOutputStream(orig);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();
        String mimeType = getFormatName();
        if (!mimeType.equals(PNG_TYPE))
            convertToPng();
        this.options = options;
        this.coords = new Double[4][2];
        for (int i = 0; i < 4; i++) {
            JSONArray vector = (JSONArray) options.coords.get(i);
            for (int j = 0; j < 2; j++) {
                this.coords[i][j] = (Double) vector.get(j);
            }
        }
    } catch (Exception e) {
        throw new TiltException(e);
    }
}

From source file:pl.psnc.synat.wrdz.zmd.download.adapters.HttpDownloadAdapter.java

@Override
public String downloadFile(URI uri, String relativePath) throws DownloadAdapterException {
    String cachedFilePath = getResourceCachePath(relativePath);
    checkDestinationExistence(cachedFilePath);
    DefaultHttpClient httpclient = new DefaultHttpClient();
    ReadableByteChannel rbc = null;
    FileOutputStream output = null;
    try {/*from   ww w . j  ava  2s. co  m*/
        if (usernamePasswordCredentials != null) {
            httpclient.getCredentialsProvider().setCredentials(authScope, usernamePasswordCredentials);
        }
        HttpResponse response = httpclient.execute(new HttpGet(uri));
        HttpEntity entity = response.getEntity();
        if (entity != null && response.getStatusLine().getStatusCode() == 200) {
            rbc = Channels.newChannel(entity.getContent());
            output = new FileOutputStream(cachedFilePath);
            output.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            EntityUtils.consume(entity);
        } else {
            EntityUtils.consume(entity);
            throw new DownloadAdapterException(
                    "Http error code or empty content was returned instead of resource.");
        }

    } catch (IOException e) {
        throw new DownloadAdapterException("Exception caught while downloading file contents to the cache.", e);
    } finally {
        httpclient.getConnectionManager().shutdown();
        try {
            if (rbc != null) {
                rbc.close();
            }
            if (output != null) {
                output.close();
            }
        } catch (IOException e) {
            throw new DownloadAdapterException("Exception caught while closing input/output streams.", e);
        }
    }
    return cachedFilePath;
}

From source file:control.services.DownloadPortraitsHttpServiceImpl.java

@Override
public File downloadPortraisFile(String portraitsFileName, String portraitsFolderName)
        throws FileNotFoundException {
    File file = null;//w  ww . j  a  v a2  s.c  o  m
    try {
        URL website = new URL(PROTOCOL_HOST + CLASH_HOST + PORTRAITS_PATH + portraitsFileName);
        //https://www.colorado.edu/conflict/peace/download/peace.zip
        // speedtest.ftp.otenet.gr/files/test10Mb.db
        //  URL website = new URL("https://www.colorado.edu/conflict/peace/download/peace.zip");
        file = new File(portraitsFolderName + File.separator + portraitsFileName);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());

        FileOutputStream fos = new FileOutputStream(file);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();

    } catch (IOException ex) {
        LOG.error(ex);
        throw new FileNotFoundException();
    }
    return file;
}