Search in sources :

Example 31 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project cythara by gstraube.

the class FFMPEGDownloader method downloadExecutable.

private void downloadExecutable(String saveTo) {
    try {
        URL website = new URL(url);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        FileOutputStream fos = new FileOutputStream(saveTo);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 32 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project BaseProject by fly803.

the class NioFileUtiles method writeToFileNio.

public static void writeToFileNio(byte[] data, File target) throws IOException {
    FileOutputStream fo = null;
    ReadableByteChannel src = null;
    FileChannel out = null;
    try {
        src = Channels.newChannel(new ByteArrayInputStream(data));
        fo = new FileOutputStream(target);
        out = fo.getChannel();
        out.transferFrom(src, 0, data.length);
    } finally {
        if (fo != null) {
            fo.close();
        }
        if (src != null) {
            src.close();
        }
        if (out != null) {
            out.close();
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream)

Example 33 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project incubator-ratis by apache.

the class DataStreamTestUtils method createFile.

static void createFile(File f, int size) throws Exception {
    final ReadableByteChannel source = new ReadableByteChannel() {

        private int offset = 0;

        @Override
        public boolean isOpen() {
            return offset < size;
        }

        @Override
        public void close() {
            offset = size;
        }

        @Override
        public int read(ByteBuffer dst) {
            final int start = offset;
            for (; dst.remaining() > 0 && isOpen(); offset++) {
                dst.put(pos2byte(offset));
            }
            return offset - start;
        }
    };
    FileUtils.createDirectories(f.getParentFile());
    try (FileOutputStream out = new FileOutputStream(f)) {
        final long transferred = out.getChannel().transferFrom(source, 0, size);
        Assert.assertEquals(size, transferred);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream) ByteBuffer(java.nio.ByteBuffer) DataStreamReplyByteBuffer(org.apache.ratis.datastream.impl.DataStreamReplyByteBuffer) DataStreamRequestByteBuffer(org.apache.ratis.datastream.impl.DataStreamRequestByteBuffer)

Example 34 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project async-http-client by AsyncHttpClient.

the class InputStreamMultipartPart method transferContentTo.

@Override
protected long transferContentTo(WritableByteChannel target) throws IOException {
    ReadableByteChannel channel = getChannel();
    ByteBuffer buffer = getBuffer();
    int transferred = 0;
    int read = channel.read(buffer);
    if (read > 0) {
        buffer.flip();
        while (buffer.hasRemaining()) {
            transferred += target.write(buffer);
        }
        buffer.compact();
        position += transferred;
    }
    if (position == getContentLength() || read < 0) {
        state = MultipartState.POST_CONTENT;
        if (channel.isOpen()) {
            channel.close();
        }
    }
    return transferred;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 35 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project archi by archimatetool.

the class CanvasDNDEditPolicy method getURLDropCommand.

/**
 * Get a Command based on URL transfer
 */
private Command getURLDropCommand(DiagramDropRequest request) throws IOException, URISyntaxException {
    String s = (String) request.getData();
    if (s == null) {
        return null;
    }
    // Bug on Linux - URL is duplicated with a newline, or image info is appended after a newline
    if (PlatformUtils.isLinux()) {
        s = s.split("\n")[0];
    }
    s = s.replaceAll(" ", "%20");
    if (!isImagePath(s)) {
        return null;
    }
    File file = null;
    URL url = new URL(s);
    // Local file:/// URL for a file that exists
    if ("file".equals(url.getProtocol())) {
        file = new File(url.toURI());
    } else // Online URL that we should download to a temporary file
    {
        // Download and save to temporary file
        file = File.createTempFile("archi-", s.substring(s.lastIndexOf(".")));
        file.deleteOnExit();
        try (FileOutputStream fos = new FileOutputStream(file)) {
            try (ReadableByteChannel rbc = Channels.newChannel(url.openStream())) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
        }
    }
    List<File> files = new ArrayList<>();
    if (file.exists() && file.canRead()) {
        files.add(file);
    }
    return createImageFileDropCommand(files, getDropLocation(request));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)307 ByteBuffer (java.nio.ByteBuffer)111 IOException (java.io.IOException)84 FileOutputStream (java.io.FileOutputStream)62 WritableByteChannel (java.nio.channels.WritableByteChannel)62 Test (org.junit.Test)52 File (java.io.File)50 FileChannel (java.nio.channels.FileChannel)49 FileInputStream (java.io.FileInputStream)43 ByteArrayInputStream (java.io.ByteArrayInputStream)38 InputStream (java.io.InputStream)36 URL (java.net.URL)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Path (java.nio.file.Path)18 Test (org.testng.annotations.Test)14 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)11 MalformedURLException (java.net.MalformedURLException)11 Vector (java.util.Vector)11