Search in sources :

Example 26 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project ddf by codice.

the class DownloadManager method run.

@Override
public void run() {
    String mimeType = null;
    try (ReadableByteChannel byteChannel = Channels.newChannel(url.openStream())) {
        mimeType = url.openConnection().getContentType();
        String fileExtension = allTypes.forName(mimeType).getExtension();
        LOGGER.debug("downloading product from: {}", url.toString());
        LOGGER.debug("mimetype is: {}", mimeType);
        LOGGER.debug("File Extension is: {}", fileExtension);
        try (FileOutputStream fileOutputStream = new FileOutputStream(outputFileName + fileExtension)) {
            fileOutputStream.getChannel().transferFrom(byteChannel, 0, Long.MAX_VALUE);
        } catch (IOException e) {
            LOGGER.info("Error opening stream for {}", outputFileName, e);
        }
    } catch (IOException e) {
        LOGGER.info("Error downloading file from url: {}", url, e);
    } catch (MimeTypeException e) {
        LOGGER.info("Error determining file extension from mimetype: {}", mimeType, e);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MimeTypeException(org.apache.tika.mime.MimeTypeException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 27 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project Gargoyle by callakrsos.

the class ChannelsExam method catConsumer.

public void catConsumer(InputStream src, WritableByteChannel dest) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
    ReadableByteChannel newChannel = Channels.newChannel(src);
    while (newChannel.read(buffer) != -1) {
        buffer.flip();
        dest.write(buffer);
        buffer.compact();
    }
    buffer.flip();
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 28 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project pravega by pravega.

the class FileSystemStorage method doWrite.

private Void doWrite(SegmentHandle handle, long offset, InputStream data, int length) throws Exception {
    long traceId = LoggerHelpers.traceEnter(log, "write", handle.getSegmentName(), offset, length);
    Timer timer = new Timer();
    if (handle.isReadOnly()) {
        throw new IllegalArgumentException("Write called on a readonly handle of segment " + handle.getSegmentName());
    }
    Path path = Paths.get(config.getRoot(), handle.getSegmentName());
    // This means that writes to readonly files also succeed. We need to explicitly check permissions in this case.
    if (!isWritableFile(path)) {
        throw new StreamSegmentSealedException(handle.getSegmentName());
    }
    long fileSize = path.toFile().length();
    if (fileSize < offset) {
        throw new BadOffsetException(handle.getSegmentName(), fileSize, offset);
    } else {
        long totalBytesWritten = 0;
        try (FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE)) {
            // Wrap the input data into a ReadableByteChannel, but do not close it. Doing so will result in closing
            // the underlying InputStream, which is not desirable if it is to be reused.
            ReadableByteChannel sourceChannel = Channels.newChannel(data);
            while (length != 0) {
                long bytesWritten = channel.transferFrom(sourceChannel, offset, length);
                assert bytesWritten > 0 : "Unable to make any progress transferring data.";
                offset += bytesWritten;
                totalBytesWritten += bytesWritten;
                length -= bytesWritten;
            }
        }
        FileSystemMetrics.WRITE_LATENCY.reportSuccessEvent(timer.getElapsed());
        FileSystemMetrics.WRITE_BYTES.add(totalBytesWritten);
        LoggerHelpers.traceLeave(log, "write", traceId);
        return null;
    }
}
Also used : Path(java.nio.file.Path) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Timer(io.pravega.common.Timer) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) FileChannel(java.nio.channels.FileChannel) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException)

Example 29 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project Payara by payara.

the class AnnotationDetector method containsAnnotation.

protected boolean containsAnnotation(InputStream is, long size) throws IOException {
    boolean result = false;
    // check if it contains top level annotations...
    ReadableByteChannel channel = null;
    try {
        channel = Channels.newChannel(is);
        if (channel != null) {
            result = classFile.containsAnnotation(channel, size);
        }
        return result;
    } finally {
        if (channel != null) {
            channel.close();
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel)

Example 30 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project S-argo by Expugn.

the class Update method updateBanners.

/**
 * Downloads the Banners.xml file from the GitHubDataRepository.
 *
 * @throws IOException
 */
private void updateBanners() throws IOException {
    boolean dataFolderExists = new File("data").exists();
    try {
        if (!dataFolderExists) {
            new File("data").mkdir();
        }
        URL website = new URL(gitHubDataRepository + "data/Banners.xml");
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        FileOutputStream fos = new FileOutputStream("data/Banners.xml");
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    } catch (MalformedURLException e) {
        throw new MalformedURLException();
    } catch (IOException e) {
        throw new IOException();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) 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