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