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