Search in sources :

Example 61 with SeekableByteChannel

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);
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SeekableByteChannel(java.nio.channels.SeekableByteChannel) ImageInputStream(javax.imageio.stream.ImageInputStream) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 62 with SeekableByteChannel

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);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) StandardOpenOption(java.nio.file.StandardOpenOption) ByteBuffer(java.nio.ByteBuffer)

Example 63 with SeekableByteChannel

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);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) InputStream(java.io.InputStream) IOException(java.io.IOException) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 64 with SeekableByteChannel

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);
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) FileChannel(java.nio.channels.FileChannel) FileAttribute(java.nio.file.attribute.FileAttribute) Test(org.junit.Test)

Example 65 with SeekableByteChannel

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());
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)151 ByteBuffer (java.nio.ByteBuffer)72 Path (java.nio.file.Path)56 IOException (java.io.IOException)52 Test (org.junit.Test)41 InputStream (java.io.InputStream)17 ReadableByteChannel (java.nio.channels.ReadableByteChannel)13 NoSuchFileException (java.nio.file.NoSuchFileException)13 FileSystem (java.nio.file.FileSystem)12 StandardOpenOption (java.nio.file.StandardOpenOption)11 Test (org.testng.annotations.Test)9 WritableByteChannel (java.nio.channels.WritableByteChannel)8 OpenOption (java.nio.file.OpenOption)8 HashSet (java.util.HashSet)8 OutputStream (java.io.OutputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 URI (java.net.URI)5