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:org.basinmc.maven.plugins.minecraft.AbstractArtifactMojo.java

/**
 * Fetches any resource from a remote HTTP server and writes it to a supplied output stream.
 *//*from  w  w  w . j a  v a 2  s  .c  om*/
protected void fetch(@Nonnull URI uri, @Nonnull @WillNotClose OutputStream outputStream) throws IOException {
    this.fetch(uri, Channels.newChannel(outputStream));
}

From source file:net.vexelon.myglob.utils.Utils.java

/**
 * Reads an input stream into a byte array
 * @param source/*from ww w  .j a  v a  2  s  .  c  o  m*/
 * @return Byte array of input stream data
 * @throws IOException
 */
public static byte[] read(InputStream source) throws IOException {
    ReadableByteChannel srcChannel = Channels.newChannel(source);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(
            source.available() > 0 ? source.available() : BUFFER_PAGE_SIZE);
    WritableByteChannel destination = Channels.newChannel(baos);

    try {
        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_PAGE_SIZE);
        while (srcChannel.read(buffer) > 0) {
            buffer.flip();
            while (buffer.hasRemaining()) {
                destination.write(buffer);
            }
            buffer.clear();
        }
        return baos.toByteArray();
    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (srcChannel != null)
                srcChannel.close();
        } catch (IOException e) {
        }
        try {
            if (source != null)
                source.close();
        } catch (IOException e) {
        }
        try {
            if (destination != null)
                destination.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.chiorichan.updater.Download.java

@Override
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;/*from  w  w w  . j a va2  s. co m*/
    try {
        HttpURLConnection conn = NetworkFunc.openHttpConnection(url);
        int response = conn.getResponseCode();
        int responseFamily = response / 100;

        if (responseFamily == 3)
            throw new DownloadException(
                    "The server issued a redirect response which the Updater 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;

        stateDone();
    } catch (DownloadDeniedException 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);
    }

    if (exception != null)
        AutoUpdater.getLogger().severe("Download Resulted in an Exception", exception);
}

From source file:com.feilong.commons.core.io.IOWriteUtil.java

/**
 * NIO API ?? ()./* w w  w. j ava2  s . com*/
 *
 * @param bufferLength
 *            the buffer length
 * @param inputStream
 *            the input stream
 * @param outputStream
 *            the output stream
 * @throws UncheckedIOException
 *             the unchecked io exception
 * @since 1.0.8
 */
private static void writeUseNIO(int bufferLength, InputStream inputStream, OutputStream outputStream)
        throws UncheckedIOException {
    int i = 0;
    int sumSize = 0;
    int j = 0;

    ///2 
    //As creme de la creme with regard to performance, you could use NIO Channels and ByteBuffer. 

    ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
    WritableByteChannel writableByteChannel = Channels.newChannel(outputStream);

    ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength);

    try {
        while (readableByteChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            j = writableByteChannel.write(byteBuffer);
            sumSize += j;
            byteBuffer.clear();
            i++;
        }

        if (log.isDebugEnabled()) {
            log.debug("Write data over,sumSize:[{}],bufferLength:[{}],loopCount:[{}]",
                    FileUtil.formatSize(sumSize), bufferLength, i);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        try {
            if (writableByteChannel != null) {
                outputStream.close();
                writableByteChannel.close();
            }
            if (readableByteChannel != null) {
                inputStream.close();
                readableByteChannel.close();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}

From source file:net.lyonlancer5.mcmp.karasu.util.ModFileUtils.java

static long download(String url, File output) throws IOException {
    URL url1 = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(url1.openStream());
    FileOutputStream fos = new FileOutputStream(output);

    if (!output.exists()) {
        output.getParentFile().mkdirs();
        output.createNewFile();//from w ww. jav  a2 s. c om
    }

    long f = fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    return f;
}

From source file:dk.dma.ais.downloader.QueryService.java

/**
 * Asynchronously loads the given file//from   www . ja va2s  .  com
 * @param url the URL to load
 * @param path the path to save the file to
 */
private Future<Path> asyncLoadFile(final String url, final Path path) {
    Callable<Path> job = () -> {
        long t0 = System.currentTimeMillis();

        // For the resulting file, drop the ".download" suffix
        String name = path.getFileName().toString();
        name = name.substring(0, name.length() - DOWNLOAD_SUFFIX.length());

        try {

            // Set up a few timeouts and fetch the attachment
            URLConnection con = new URL(url).openConnection();
            con.setConnectTimeout(60 * 1000); // 1 minute
            con.setReadTimeout(60 * 60 * 1000); // 1 hour

            if (!StringUtils.isEmpty(authHeader)) {
                con.setRequestProperty("Authorization", authHeader);
            }

            try (ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
                    FileOutputStream fos = new FileOutputStream(path.toFile())) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
            log.info(String.format("Copied %s -> %s in %d ms", url, path, System.currentTimeMillis() - t0));

        } catch (Exception e) {
            log.log(Level.SEVERE, "Failed downloading " + url + ": " + e.getMessage());

            // Delete the old file
            if (Files.exists(path)) {
                try {
                    Files.delete(path);
                } catch (IOException e1) {
                    log.finer("Failed deleting old file " + path);
                }
            }

            // Save an error file
            Path errorFile = path.getParent().resolve(name + ".err.txt");
            try (PrintStream err = new PrintStream(new FileOutputStream(errorFile.toFile()))) {
                e.printStackTrace(err);
            } catch (IOException ex) {
                log.finer("Failed generating error file " + errorFile);
            }
            return errorFile;
        }

        Path resultPath = path.getParent().resolve(name);
        try {
            Files.move(path, resultPath);
        } catch (IOException e) {
            log.log(Level.SEVERE, "Failed renaming path " + path + ": " + e.getMessage());
        }
        return resultPath;
    };

    log.info("Submitting new job: " + url);
    return processPool.submit(job);
}

From source file:org.alfresco.cacheserver.PatchServiceRESTTest.java

public PatchDocument getPatch(MultiPart resource) throws IOException {
    Integer blockSize = null;/*www. j av a 2  s .  c  om*/
    Integer matchCount = null;

    List<Integer> matchedBlocks = null;
    List<Patch> patches = new LinkedList<>();

    int c = 0;
    InputStream is = null;
    Integer size = null;
    Integer lastMatchIndex = null;

    // This will iterate the individual parts of the multipart response
    for (BodyPart bodyPart : resource.getBodyParts()) {
        //           if(bodyPart instanceof FormDataMultiPart)
        //           {
        //               System.out.printf(
        //                       "Multipart Body Part [Mime Type: %s]\n",
        //                       bodyPart.getMediaType());

        //               FormDataMultiPart mp = (FormDataMultiPart)bodyPart;
        //               for (BodyPart bodyPart1 : mp.getBodyParts())
        //               {
        ContentDisposition contentDisposition = bodyPart.getContentDisposition();
        //                   if(contentDisposition instanceof FormDataContentDisposition)
        //                   {
        //                       FormDataContentDisposition cd = (FormDataContentDisposition)contentDisposition;
        Map<String, String> parameters = contentDisposition.getParameters();
        String name = parameters.get("name");
        MediaType mediaType = bodyPart.getMediaType();
        //                   System.out.println("Body Part " + name);

        if (name.equals("p_size")) {
            String s = getAsString(bodyPart);
            size = Integer.parseInt(s);
            c++;
        } else if (name.equals("p_last_match_idx")) {
            String s = getAsString(bodyPart);
            lastMatchIndex = Integer.parseInt(s);
            c++;
        } else if (name.equals("p_stream")) {
            BodyPartEntity bpEntity = (BodyPartEntity) bodyPart.getEntity();
            is = bpEntity.getInputStream();
            c++;
        } else if (name.equals("p_block_size")) {
            String s = getAsString(bodyPart);
            blockSize = Integer.parseInt(s);
        } else if (name.equals("p_match_count")) {
            String s = getAsString(bodyPart);
            matchCount = Integer.parseInt(s);
        }

        if (c >= 3) {
            c = 0;
            ByteBuffer bb = ByteBuffer.allocate(1024 * 20); // TODO
            ReadableByteChannel channel = Channels.newChannel(is);
            channel.read(bb);
            bb.flip();
            byte[] buffer = new byte[bb.limit()];
            bb.get(buffer);
            Patch patch = new Patch(lastMatchIndex, size, buffer);
            patches.add(patch);
        }
        //                   }
        //               }

        //               ByteBuffer bb = ByteBuffer.allocate(1024*20); // TODO
        //               ReadableByteChannel channel = Channels.newChannel(is);
        //               channel.read(bb);
        //               bb.flip();
        //               byte[] buffer = new byte[bb.limit()];
        //               bb.get(buffer);
        //               Patch patch = new Patch(lastMatchIndex, size, buffer);
        //               patches.add(patch);
    }
    //           else
    //           {
    //               System.out.printf(
    //                       "Embedded Body Part [Mime Type: %s, Length: %s]\n",
    //                       bodyPart.getMediaType(), bodyPart.getContentDisposition().getSize());
    //
    //               ContentDisposition contentDisposition = bodyPart.getContentDisposition();
    //               Map<String, String> parameters = contentDisposition.getParameters();
    //               String name = parameters.get("name");
    ////               if(contentDisposition instanceof FormDataContentDisposition)
    ////               {
    ////                   FormDataContentDisposition cd = (FormDataContentDisposition)contentDisposition;
    ////                   String name = cd.getName();
    //
    //               Object entity = bodyPart.getEntity();
    ////               if(entity instanceof BodyPartEntity)
    ////               {
    ////                   BodyPartEntity bpEntity = (BodyPartEntity)entity;
    ////                   if(name.equals("p_block_size"))
    ////                   {
    ////                       blockSize = Integer.parseInt((String)entity);
    ////                   }
    ////                   else if(name.equals("p_match_count"))
    ////                   {
    ////                       matchCount = Integer.parseInt((String)bodyPart.getEntity());
    ////                   }
    ////                   else if(name.equals("p_matched_blocks"))
    ////                   {
    ////                       String matchedBlocksStr = (String)bodyPart.getEntity();
    ////                       List<String> l = Arrays.asList(matchedBlocksStr.split(","));
    ////                       matchedBlocks = l.stream()
    ////                               .filter(s -> s != null && !s.equals(""))
    ////                               .map(s -> Integer.parseInt(s))
    ////                               .collect(Collectors.toList());
    ////                   }
    ////               }
    //           }
    //       }

    PatchDocument patchDocument = new PatchDocument(blockSize, matchedBlocks, patches);
    return patchDocument;
}

From source file:prototypes.ws.proxy.soap.commons.io.Files.java

public static String download(String httpPath) {
    String localPath = "";
    LOGGER.debug("Remote file to download : {}", httpPath);
    try {//from   ww  w.  jav a 2s. c om
        File folder = createTempDirectory();
        if (!folder.exists()) {
            folder.mkdir();
        }
        URL url = new URL(httpPath);
        String distantFile = url.getFile();
        if (distantFile != null) {
            int pos = distantFile.lastIndexOf('/');
            if (pos != -1) {
                distantFile = distantFile.substring(pos + 1, distantFile.length());
            }
        }
        localPath = folder.getAbsolutePath() + File.separator + distantFile;
        LOGGER.debug("Local path to save to : {}", localPath);
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fos = new FileOutputStream(localPath);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        LOGGER.info("Remote file downloaded.");
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage(), ex);
    }
    return localPath;
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnNewJavaFieldCutter.java

public void cutColumns(InputStream in, int noCardsPerCase, int caseLength, String delimitor, String tabFileName)
        throws IOException {

    if (delimitor == null) {
        delimitor = defaultDelimitor;//from w w  w  .  java2 s.c  om
    }

    OUT_LEN = colwidth; // calculated by parseList
    dbgLog.fine("out_len=" + OUT_LEN);

    String firstline = null;

    if (caseLength == 0) {

        int cread;
        int ccounter = 0;

        firstline = "";

        while (caseLength == 0 && (cread = in.read()) != -1) {
            ccounter++;
            if (cread == '\n') {
                caseLength = ccounter;
            }
            char c = (char) cread;
            firstline = firstline + c;
        }

    }

    if (caseLength == 0) {
        throw new IOException("Subsetting failed: could not read incoming byte stream. "
                + "(Requested file may be unavailable or missing)");

    }

    REC_LEN = caseLength;
    dbgLog.fine("REC_LEN=" + REC_LEN);

    for (int i = 0; i < cargSet.get(Long.valueOf(noCardsPerCase)).size(); i++) {
        int varEndOffset = cargSet.get(Long.valueOf(noCardsPerCase)).get(i).get(1);

        if (REC_LEN <= varEndOffset + 1) {
            throw new IOException("Failed to subset incoming byte stream. Invalid input. "
                    + "(Detected the first record of " + REC_LEN + " bytes; "
                    + "one of the columns requested ends at " + varEndOffset + " bytes).");
        }
    }

    Boolean dottednotation = false;
    Boolean foundData = false;

    // cutting a data file

    ReadableByteChannel rbc = Channels.newChannel(in);
    // input byte-buffer size = row-length + 1(=> new line char)
    ByteBuffer inbuffer = ByteBuffer.allocate(REC_LEN);

    OutputStream outs = new FileOutputStream(tabFileName);
    WritableByteChannel outc = Channels.newChannel(outs);
    ByteBuffer outbuffer = null;

    int pos = 0;
    int offset = 0;
    int outoffset = 0;

    int begin = 0;
    int end = 0;
    int blankoffset = 0;

    int blanktail = 0;
    int k;

    try {
        // lc: line counter
        int lc = 0;
        while (firstline != null || rbc.read(inbuffer) != -1) {

            if (firstline != null) {
                // we have the first line saved as a String:
                inbuffer.put(firstline.getBytes());
                firstline = null;
            }

            // calculate i-th card number
            lc++;
            k = lc % noCardsPerCase;
            if (k == 0) {
                k = noCardsPerCase;
            }
            //out.println("***** " +lc+ "-th line, recod k=" + k + " *****");
            byte[] line_read = new byte[OUT_LEN];
            byte[] junk = new byte[REC_LEN];
            byte[] line_final = new byte[OUT_LEN];

            //out.println("READ: " + offset);
            inbuffer.rewind();

            offset = 0;
            outoffset = 0;

            // how many variables are cut from this k-th card
            int noColumns = cargSet.get(Long.valueOf(k)).size();

            //out.println("noColumns=" + noColumns);
            //out.println("cargSet k =" + cargSet.get(Long.valueOf(k)));

            for (int i = 0; i < noColumns; i++) {
                //out.println("**** " + i +"-th col ****");
                begin = cargSet.get(Long.valueOf(k)).get(i).get(0); // bounds[2 * i];
                end = cargSet.get(Long.valueOf(k)).get(i).get(1); // bounds[2 * i + 1];

                //out.println("i: begin: " + begin + "\ti: end:" + end);

                try {
                    // throw away offect bytes
                    if (begin - offset - 1 > 0) {
                        inbuffer.get(junk, 0, (begin - offset - 1));
                    }
                    // get requested bytes
                    inbuffer.get(line_read, outoffset, (end - begin + 1));
                    // set outbound data
                    outbounds[2 * i] = outoffset;
                    outbounds[2 * i + 1] = outoffset + (end - begin);
                    // current position moved to outoffset
                    pos = outoffset;

                    dottednotation = false;
                    foundData = false;

                    blankoffset = 0;
                    blanktail = 0;

                    // as position increases
                    while (pos <= (outoffset + (end - begin))) {

                        //out.println("pos=" + pos + "\tline_read[pos]=" +
                        //    new String(line_read).replace("\000", "\052"));

                        // decimal octal
                        // 48 =>0 60
                        // 46 => . 56
                        // 32 = space 40

                        // dot: 
                        if (line_read[pos] == '\056') {
                            dottednotation = true;
                        }

                        // space:
                        if (line_read[pos] == '\040') {
                            if (foundData) {
                                blanktail = blanktail > 0 ? blanktail : pos - 1;
                            } else {
                                blankoffset = pos + 1;
                            }
                        } else {
                            foundData = true;
                            blanktail = 0;
                        }

                        pos++;
                    }
                    // increase the outoffset by width
                    outoffset += (end - begin + 1);
                    // dot false
                    if (!dottednotation) {
                        if (blankoffset > 0) {
                            // set outbound value to blankoffset
                            outbounds[2 * i] = blankoffset;
                        }
                        if (blanktail > 0) {
                            outbounds[2 * i + 1] = blanktail;
                        }
                    }

                } catch (BufferUnderflowException bufe) {
                    //bufe.printStackTrace();
                    throw new IOException(bufe.getMessage());
                }
                // set offset to the value of end-position
                offset = end;
            }

            outoffset = 0;
            // for each var
            for (int i = 0; i < noColumns; i++) {
                begin = outbounds[2 * i];
                end = outbounds[2 * i + 1];
                //out.println("begin=" + begin + "\t end=" + end);
                for (int j = begin; j <= end; j++) {
                    line_final[outoffset++] = line_read[j];
                }

                if (i < (noColumns - 1)) {
                    line_final[outoffset++] = '\011'; // tab x09
                } else {
                    if (k == cargSet.size()) {
                        line_final[outoffset++] = '\012'; // LF x0A
                    } else {
                        line_final[outoffset++] = '\011'; // tab x09
                    }
                }
            }
            //out.println("line_final=" +
            //    new String(line_final).replace("\000", "\052"));
            outbuffer = ByteBuffer.wrap(line_final, 0, outoffset);
            outc.write(outbuffer);
            inbuffer.clear();

        } // while loop
    } catch (IOException ex) {
        //ex.printStackTrace();
        throw new IOException("Failed to subset incoming fixed-field stream: " + ex.getMessage());
    }

}

From source file:org.fao.geonet.utils.BinaryFile.java

/**
 * Copies an input stream (from a file) to an output stream
 *//*from   w  w w.j  av a 2  s  .  c  o  m*/
public static void copy(InputStream in, OutputStream out) throws IOException {
    if (in instanceof FileInputStream) {
        FileInputStream fin = (FileInputStream) in;
        WritableByteChannel outChannel;
        if (out instanceof FileOutputStream) {
            outChannel = ((FileOutputStream) out).getChannel();
        } else {
            outChannel = Channels.newChannel(out);
        }
        fin.getChannel().transferTo(0, Long.MAX_VALUE, outChannel);
    } else {
        BufferedInputStream input = new BufferedInputStream(in);

        byte buffer[] = new byte[BUF_SIZE];
        int nRead;

        while ((nRead = input.read(buffer)) > 0)
            out.write(buffer, 0, nRead);
    }
}