use of java.nio.channels.SeekableByteChannel in project mycore by MyCoRe-Org.
the class MCRIView2Tools method readTile.
public static BufferedImage readTile(Path iviewFileRoot, ImageReader imageReader, int zoomLevel, int x, int y) throws IOException {
String tileName = MessageFormat.format("{0}/{1}/{2}.jpg", zoomLevel, y, x);
Path tile = iviewFileRoot.resolve(tileName);
if (Files.exists(tile)) {
try (SeekableByteChannel fileChannel = Files.newByteChannel(tile)) {
ImageInputStream iis = ImageIO.createImageInputStream(fileChannel);
if (iis == null) {
throw new IOException("Could not acquire ImageInputStream from SeekableByteChannel: " + tile);
}
imageReader.setInput(iis, true);
BufferedImage image = imageReader.read(0);
imageReader.reset();
iis.close();
return image;
}
} else {
throw new NoSuchFileException(iviewFileRoot.toString(), tileName, null);
}
}
use of java.nio.channels.SeekableByteChannel in project graal by oracle.
the class FileSystemsTest method write.
private static void write(Path path, byte[] content, FileSystem fs) throws IOException {
final Set<StandardOpenOption> options = EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
try (SeekableByteChannel ch = fs.newByteChannel(path, options)) {
ByteBuffer bb = ByteBuffer.wrap(content);
ch.write(bb);
}
}
use of java.nio.channels.SeekableByteChannel in project graal by oracle.
the class TruffleFile method readAllBytes.
/**
* Reads a file content as bytes.
*
* @return the created <code>byte[]</code>
* @throws IOException in case of IO error
* @throws OutOfMemoryError if an array of a file size cannot be allocated
* @throws SecurityException if the {@link FileSystem} denied the operation
* @since 19.0
*/
@TruffleBoundary
public byte[] readAllBytes() throws IOException {
try (SeekableByteChannel channel = newByteChannel(Collections.emptySet())) {
long sizel = channel.size();
if (sizel > MAX_BUFFER_SIZE) {
throw new OutOfMemoryError("File size is too large.");
}
try (InputStream in = Channels.newInputStream(channel)) {
int size = (int) sizel;
byte[] buf = new byte[size];
int read = 0;
while (true) {
int n;
while ((n = in.read(buf, read, size - read)) > 0) {
read += n;
}
if (n < 0 || (n = in.read()) < 0) {
break;
}
if (size << 1 <= MAX_BUFFER_SIZE) {
size = Math.max(size << 1, BUFFER_SIZE);
} else if (size == MAX_BUFFER_SIZE) {
throw new OutOfMemoryError("Required array size too large");
} else {
size = MAX_BUFFER_SIZE;
}
buf = Arrays.copyOf(buf, size);
buf[read++] = (byte) n;
}
return size == read ? buf : Arrays.copyOf(buf, read);
}
} catch (IOException | OutOfMemoryError | SecurityException e) {
throw e;
} catch (Throwable t) {
throw wrapHostException(t);
}
}
use of java.nio.channels.SeekableByteChannel in project graal by oracle.
the class NIOFileSystemTest method testNewByteChannel.
@Test
public void testNewByteChannel() throws IOException {
expectException(() -> fs.newByteChannel(null, Collections.emptySet()).close(), NullPointerException.class);
expectException(() -> fs.newByteChannel(file, null).close(), NullPointerException.class);
expectException(() -> fs.newByteChannel(file, Collections.singleton(null)).close(), NullPointerException.class);
Path targetRelative = workDir.resolve("testNewByteChannel");
expectException(() -> fs.newByteChannel(targetRelative, EnumSet.of(StandardOpenOption.CREATE), (FileAttribute<?>[]) null).close(), NullPointerException.class);
expectException(() -> fs.newByteChannel(targetRelative, EnumSet.of(StandardOpenOption.CREATE), new FileAttribute<?>[] { null }).close(), NullPointerException.class);
expectException(() -> fs.newByteChannel(targetRelative, EnumSet.of(StandardOpenOption.CREATE), ATTR_UNKNOWN).close(), UnsupportedOperationException.class);
expectException(() -> fs.newByteChannel(file, EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW)).close(), FileAlreadyExistsException.class);
expectException(() -> fs.newByteChannel(targetRelative, Collections.emptySet()).close(), NoSuchFileException.class);
try (SeekableByteChannel ch = fs.newByteChannel(targetRelative, EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE))) {
Assert.assertTrue(ch instanceof FileChannel);
}
try (SeekableByteChannel ch = fs.newByteChannel(file, EnumSet.of(StandardOpenOption.READ))) {
Assert.assertTrue(ch instanceof FileChannel);
}
try (SeekableByteChannel ch = fs.newByteChannel(fileRelative, EnumSet.of(StandardOpenOption.READ))) {
Assert.assertTrue(ch instanceof FileChannel);
}
}
use of java.nio.channels.SeekableByteChannel in project graal by oracle.
the class FileSystemProviderTest method testNewByteChannel.
@Test
public void testNewByteChannel() throws IOException {
try (SeekableByteChannel ch = fs.newByteChannel(existingAbsolute, EnumSet.of(StandardOpenOption.READ))) {
final ByteBuffer buffer = ByteBuffer.allocate(1024);
ch.read(buffer);
buffer.flip();
Assert.assertEquals(getClass().getSimpleName(), StandardCharsets.UTF_8.decode(buffer).toString());
}
try (SeekableByteChannel ch = fs.newByteChannel(existingRelative, EnumSet.of(StandardOpenOption.READ))) {
final ByteBuffer buffer = ByteBuffer.allocate(1024);
ch.read(buffer);
buffer.flip();
Assert.assertEquals(getClass().getSimpleName(), StandardCharsets.UTF_8.decode(buffer).toString());
}
}
Aggregations