use of java.nio.channels.ReadableByteChannel in project sis by apache.
the class StorageConnectorTest method testGetAsDataInput.
/**
* Implementation of {@link #testGetAsDataInputFromURL()} and {@link #testGetAsDataInputFromStream()}.
*/
private void testGetAsDataInput(final boolean asStream) throws DataStoreException, IOException {
final StorageConnector connection = create(asStream);
final DataInput input = connection.getStorageAs(DataInput.class);
assertSame("Value shall be cached.", input, connection.getStorageAs(DataInput.class));
assertInstanceOf("Needs the SIS implementation.", ChannelImageInputStream.class, input);
assertSame("Instance shall be shared.", input, connection.getStorageAs(ChannelDataInput.class));
/*
* Reads a single integer for checking that the stream is at the right position, then close the stream.
* Since the file is a compiled Java class, the integer that we read shall be the Java magic number.
*/
final ReadableByteChannel channel = ((ChannelImageInputStream) input).channel;
assertTrue("channel.isOpen()", channel.isOpen());
assertEquals("First 4 bytes", MAGIC_NUMBER, input.readInt());
connection.closeAllExcept(null);
assertFalse("channel.isOpen()", channel.isOpen());
}
use of java.nio.channels.ReadableByteChannel in project lwjgl3-demos by LWJGL.
the class DemoUtils method ioResourceToByteBuffer.
/**
* Reads the specified resource and returns the raw data as a ByteBuffer.
*
* @param resource the resource to read
* @param bufferSize the initial buffer size
*
* @return the resource data
*
* @throws IOException if an IO error occurs
*/
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
ByteBuffer buffer;
URL url = Thread.currentThread().getContextClassLoader().getResource(resource);
File file = new File(url.getFile());
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
fc.close();
fis.close();
} else {
buffer = BufferUtils.createByteBuffer(bufferSize);
InputStream source = url.openStream();
if (source == null)
throw new FileNotFoundException(resource);
try {
ReadableByteChannel rbc = Channels.newChannel(source);
try {
while (true) {
int bytes = rbc.read(buffer);
if (bytes == -1)
break;
if (buffer.remaining() == 0)
buffer = resizeBuffer(buffer, buffer.capacity() * 2);
}
buffer.flip();
} finally {
rbc.close();
}
} finally {
source.close();
}
}
return buffer;
}
use of java.nio.channels.ReadableByteChannel in project android_packages_apps_Updater by LineageOS.
the class FileUtils method copyFile.
public static void copyFile(File sourceFile, File destFile, ProgressCallBack progressCallBack) throws IOException {
try (FileChannel sourceChannel = new FileInputStream(sourceFile).getChannel();
FileChannel destChannel = new FileOutputStream(destFile).getChannel()) {
if (progressCallBack != null) {
ReadableByteChannel readableByteChannel = new CallbackByteChannel(sourceChannel, sourceFile.length(), progressCallBack);
destChannel.transferFrom(readableByteChannel, 0, sourceChannel.size());
} else {
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}
} catch (IOException e) {
Log.e(TAG, "Could not copy file", e);
if (destFile.exists()) {
destFile.delete();
}
throw e;
}
}
use of java.nio.channels.ReadableByteChannel in project i2p.i2p by i2p.
the class SAMv3StreamSession method connect.
/**
* Connect the SAM STREAM session to the specified Destination
* for a single connection, using the socket stolen from the handler.
*
* @param handler The handler that communicates with the requesting client
* @param dest Base64-encoded Destination to connect to
* @param props Options to be used for connection
*
* @throws DataFormatException if the destination is not valid
* @throws ConnectException if the destination refuses connections
* @throws NoRouteToHostException if the destination can't be reached
* @throws InterruptedIOException if the connection timeouts
* @throws I2PException if there's another I2P-related error
* @throws IOException
*/
public void connect(SAMv3Handler handler, String dest, Properties props) throws I2PException, ConnectException, NoRouteToHostException, DataFormatException, InterruptedIOException, IOException {
boolean verbose = !Boolean.parseBoolean(props.getProperty("SILENT"));
Destination d = SAMUtils.getDest(dest);
I2PSocketOptions opts = socketMgr.buildOptions(props);
if (props.getProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT) == null)
opts.setConnectTimeout(60 * 1000);
String fromPort = props.getProperty("FROM_PORT");
if (fromPort != null) {
try {
opts.setLocalPort(Integer.parseInt(fromPort));
} catch (NumberFormatException nfe) {
throw new I2PException("Bad port " + fromPort);
}
}
String toPort = props.getProperty("TO_PORT");
if (toPort != null) {
try {
opts.setPort(Integer.parseInt(toPort));
} catch (NumberFormatException nfe) {
throw new I2PException("Bad port " + toPort);
}
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Connecting new I2PSocket...");
// blocking connection (SAMv3)
I2PSocket i2ps = socketMgr.connect(d, opts);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if (rec == null)
throw new InterruptedIOException();
handler.notifyStreamResult(verbose, "OK", null);
handler.stealSocket();
ReadableByteChannel fromClient = handler.getClientSocket();
ReadableByteChannel fromI2P = Channels.newChannel(i2ps.getInputStream());
WritableByteChannel toClient = handler.getClientSocket();
WritableByteChannel toI2P = Channels.newChannel(i2ps.getOutputStream());
SAMBridge bridge = handler.getBridge();
(new I2PAppThread(rec.getThreadGroup(), new Pipe(fromClient, toI2P, bridge), "ConnectV3 SAMPipeClientToI2P")).start();
(new I2PAppThread(rec.getThreadGroup(), new Pipe(fromI2P, toClient, bridge), "ConnectV3 SAMPipeI2PToClient")).start();
}
use of java.nio.channels.ReadableByteChannel in project UnityModManager by xausky.
the class FileUtils method writeToFile.
public static void writeToFile(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();
}
}
}
Aggregations