Search in sources :

Example 36 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project jbosstools-hibernate by jbosstools.

the class ResourceFactory method downloadFile.

/**
 * Download given file
 *
 * @param sourceURL source url
 * @param target target absolute path
 * @throws IOException thrown by java IO operations
 */
private static void downloadFile(URL sourceURL, String target) throws IOException {
    ReadableByteChannel rbc = Channels.newChannel(sourceURL.openStream());
    FileOutputStream fos = new FileOutputStream(target);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream)

Example 37 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project cytoscape-impl by cytoscape.

the class WebQuerier method downloadApp.

/**
 * Given the unique app name used by the app store, query the app store for the
 * download URL and download the app to the given directory.
 *
 * If a file with the same name exists in the directory, it is overwritten.
 *
 * @param appName The unique app name used by the app store
 * @param version The desired version, or <code>null</code> to obtain the latest release
 * @param directory The directory used to store the downloaded file
 * @param taskMonitor
 */
public File downloadApp(WebApp webApp, String version, File directory, DownloadStatus status) throws AppDownloadException {
    List<WebApp.Release> compatibleReleases = getCompatibleReleases(webApp);
    if (compatibleReleases.size() > 0) {
        WebApp.Release releaseToDownload = null;
        if (version != null) {
            for (WebApp.Release compatibleRelease : compatibleReleases) {
                // Check if the desired version is found in the list of available versions
                if (compatibleRelease.getReleaseVersion().matches("(^\\s*|.*,)\\s*" + version + "\\s*(\\s*$|,.*)")) {
                    releaseToDownload = compatibleRelease;
                }
            }
            if (releaseToDownload == null) {
                throw new AppDownloadException("No release with the requested version " + version + " was found for the requested app " + webApp.getFullName());
            }
        } else {
            releaseToDownload = compatibleReleases.get(compatibleReleases.size() - 1);
        }
        URL downloadUrl = null;
        try {
            downloadUrl = new URL(currentAppStoreUrl + releaseToDownload.getRelativeUrl());
        } catch (MalformedURLException e) {
            throw new AppDownloadException("Unable to obtain URL for version " + version + " of the release for " + webApp.getFullName());
        }
        if (downloadUrl != null) {
            try {
                // Prepare to download
                URLConnection connection = streamUtil.getURLConnection(downloadUrl);
                InputStream inputStream = connection.getInputStream();
                long contentLength = connection.getContentLength();
                ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
                File outputFile;
                try {
                    // Replace spaces with underscores
                    String outputFileBasename = webApp.getName().replaceAll("\\s", "_");
                    // Append version information
                    outputFileBasename += "-v" + releaseToDownload.getReleaseVersion();
                    // Strip disallowed characters
                    outputFileBasename = OUTPUT_FILENAME_DISALLOWED_CHARACTERS.matcher(outputFileBasename).replaceAll("");
                    // Append extension
                    outputFileBasename += ".jar";
                    // Output file has same name as app, but spaces and slashes are replaced with hyphens
                    outputFile = new File(directory.getCanonicalPath() + File.separator + outputFileBasename);
                    if (outputFile.exists()) {
                        outputFile.delete();
                    }
                    outputFile.createNewFile();
                    FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
                    try {
                        FileChannel fileChannel = fileOutputStream.getChannel();
                        long currentDownloadPosition = 0;
                        long bytesTransferred;
                        TaskMonitor taskMonitor = status.getTaskMonitor();
                        do {
                            bytesTransferred = fileChannel.transferFrom(readableByteChannel, currentDownloadPosition, 1 << 14);
                            if (status.isCanceled()) {
                                outputFile.delete();
                                return null;
                            }
                            currentDownloadPosition += bytesTransferred;
                            if (contentLength > 0) {
                                double progress = (double) currentDownloadPosition / contentLength;
                                taskMonitor.setProgress(progress);
                            }
                        } while (bytesTransferred > 0);
                    } finally {
                        fileOutputStream.close();
                    }
                } finally {
                    readableByteChannel.close();
                }
                return outputFile;
            } catch (IOException e) {
                throw new AppDownloadException("Error while downloading app " + webApp.getFullName() + ", " + e.getMessage());
            }
        }
    } else {
        throw new AppDownloadException("No available releases were found for the app " + webApp.getFullName() + ".");
    }
    return null;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) Release(org.cytoscape.app.internal.net.WebApp.Release) AppDownloadException(org.cytoscape.app.internal.exception.AppDownloadException) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) TaskMonitor(org.cytoscape.work.TaskMonitor) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Release(org.cytoscape.app.internal.net.WebApp.Release)

Example 38 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project grafikon by jub77.

the class LoadSaveImages method loadTimetableImages.

/**
 * loads images for timetable.
 *
 * @param diagram train diagram
 * @param zipFile zip file
 * @throws java.io.IOException
 */
public void loadTimetableImages(TrainDiagram diagram, ZipFile zipFile) throws IOException {
    for (TimetableImage image : diagram.getImages()) {
        ZipEntry entry = zipFile.getEntry("images/" + image.getFilename());
        if (entry != null) {
            File tempFile = File.createTempFile("gt_", ".temp");
            InputStream is = zipFile.getInputStream(entry);
            ReadableByteChannel ic = Channels.newChannel(is);
            try (FileOutputStream os = new FileOutputStream(tempFile)) {
                FileChannel oc = os.getChannel();
                oc.transferFrom(ic, 0, entry.getSize());
            }
            image.setImageFile(tempFile);
            tempFile.deleteOnExit();
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) TimetableImage(net.parostroj.timetable.model.TimetableImage) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 39 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project com.revolsys.open by revolsys.

the class ChannelReader method close.

@Override
public void close() {
    final ReadableByteChannel channel = this.channel;
    this.channel = null;
    if (channel != null) {
        try {
            channel.close();
        } catch (final IOException e) {
            throw Exceptions.wrap(e);
        }
    }
    this.buffer = null;
    this.tempBuffer = null;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) IOException(java.io.IOException)

Example 40 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project com.revolsys.open by revolsys.

the class ChannelReader method read.

private void read(final int minCount) {
    final ReadableByteChannel channel = this.channel;
    final ByteBuffer buffer = this.buffer;
    int available = this.available;
    try {
        buffer.clear();
        while (available < minCount) {
            final int readCount = channel.read(buffer);
            if (readCount == -1) {
                throw new EOFException();
            } else {
                available += readCount;
            }
        }
        buffer.flip();
    } catch (final IOException e) {
        throw Exceptions.wrap(e);
    } finally {
        this.available = available;
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) EOFException(java.io.EOFException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

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