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();
}
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;
}
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();
}
}
}
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;
}
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;
}
}
Aggregations