Search in sources :

Example 36 with FileChannel

use of java.nio.channels.FileChannel in project j2objc by google.

the class FileChannelImpl method transferFrom.

public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
    checkOpen();
    if (!src.isOpen()) {
        throw new ClosedChannelException();
    }
    checkWritable();
    if (position < 0 || count < 0 || count > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("position=" + position + " count=" + count);
    }
    if (position > size()) {
        return 0;
    }
    // so the mmap(2) overhead isn't a concern.
    if (src instanceof FileChannel) {
        FileChannel fileSrc = (FileChannel) src;
        long size = fileSrc.size();
        long filePosition = fileSrc.position();
        count = Math.min(count, size - filePosition);
        ByteBuffer buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
        try {
            fileSrc.position(filePosition + count);
            return write(buffer, position);
        } finally {
            NioUtils.freeDirectBuffer(buffer);
        }
    }
    // For non-file channels, all we can do is read and write via userspace.
    ByteBuffer buffer = ByteBuffer.allocate((int) count);
    src.read(buffer);
    buffer.flip();
    return write(buffer, position);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FileChannel(java.nio.channels.FileChannel)

Example 37 with FileChannel

use of java.nio.channels.FileChannel in project SQLWindowing by hbutani.

the class PersistentByteBasedList method load.

protected static void load(ByteBasedList l, File f) throws IOException {
    int hdr = headerSize();
    FileInputStream fis = new FileInputStream(f);
    try {
        FileChannel fc = fis.getChannel();
        ByteBuffer buf0 = ByteBuffer.allocate(hdr);
        while (buf0.hasRemaining()) fc.read(buf0);
        buf0.flip();
        l.startOffset = buf0.getInt();
        l.bytesUsed = buf0.getInt();
        l.currentSize = buf0.getInt();
        l.lastModified = buf0.getLong();
        /*
			 * note: could save this space by using Memory-Mapped I/O and directly writing to the MM buffer. 
			 */
        ByteBuffer offsetB = ByteBuffer.allocate((Integer.SIZE / Byte.SIZE) * 2 * l.currentSize);
        ByteBuffer bytesB = ByteBuffer.allocate(l.bytesUsed);
        ByteBuffer[] bufs = new ByteBuffer[] { offsetB, bytesB };
        while (fc.read(bufs) > 0) ;
        l.offsetsArray = new int[l.currentSize * 2];
        offsetB.flip();
        IntBuffer iB = offsetB.asIntBuffer();
        iB.get(l.offsetsArray);
        l.bytes = bytesB.array();
    } finally {
        fis.close();
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) IntBuffer(java.nio.IntBuffer) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 38 with FileChannel

use of java.nio.channels.FileChannel in project guava by hceylan.

the class Files method map.

private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode, long size) throws IOException {
    FileChannel channel = raf.getChannel();
    boolean threw = true;
    try {
        MappedByteBuffer mbb = channel.map(mode, 0, size);
        threw = false;
        return mbb;
    } finally {
        Closeables.close(channel, threw);
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel)

Example 39 with FileChannel

use of java.nio.channels.FileChannel in project j2objc by google.

the class OldRandomAccessFileTest method test_ConstructorLjava_lang_StringLjava_lang_String.

/**
     * java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
     *        java.lang.String)
     */
public void test_ConstructorLjava_lang_StringLjava_lang_String() throws IOException {
    RandomAccessFile raf = null;
    File tmpFile = new File(fileName);
    try {
        raf = new java.io.RandomAccessFile(fileName, "r");
        fail("Test 1: FileNotFoundException expected.");
    } catch (FileNotFoundException e) {
    // Expected.
    } catch (IllegalArgumentException e) {
        fail("Test 2: Unexpected IllegalArgumentException: " + e.getMessage());
    }
    try {
        // Checking the remaining valid mode parameters.
        try {
            raf = new java.io.RandomAccessFile(fileName, "rwd");
        } catch (IllegalArgumentException e) {
            fail("Test 3: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        try {
            raf = new java.io.RandomAccessFile(fileName, "rws");
        } catch (IllegalArgumentException e) {
            fail("Test 4: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        try {
            raf = new java.io.RandomAccessFile(fileName, "rw");
        } catch (IllegalArgumentException e) {
            fail("Test 5: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        // Checking an invalid mode parameter.
        try {
            raf = new java.io.RandomAccessFile(fileName, "i");
            fail("Test 6: IllegalArgumentException expected.");
        } catch (IllegalArgumentException e) {
        // Expected.
        }
        // Checking for NoWritableChannelException.
        raf = new java.io.RandomAccessFile(fileName, "r");
        FileChannel fcr = raf.getChannel();
        try {
            fcr.lock(0L, Long.MAX_VALUE, false);
            fail("Test 7: NonWritableChannelException expected.");
        } catch (NonWritableChannelException e) {
        // Expected.
        }
    } finally {
        if (raf != null)
            raf.close();
        if (tmpFile.exists())
            tmpFile.delete();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) NonWritableChannelException(java.nio.channels.NonWritableChannelException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 40 with FileChannel

use of java.nio.channels.FileChannel in project j2objc by google.

the class FileChannelTest method test_append.

public void test_append() throws Exception {
    File tmp = File.createTempFile("FileChannelTest", "tmp");
    FileOutputStream fos = new FileOutputStream(tmp, true);
    FileChannel fc = fos.getChannel();
    fc.write(ByteBuffer.wrap("hello".getBytes("US-ASCII")));
    fc.position(0);
    // The RI reports whatever position you set...
    assertEquals(0, fc.position());
    // ...but writes to the end of the file.
    fc.write(ByteBuffer.wrap(" world".getBytes("US-ASCII")));
    fos.close();
    assertEquals("hello world", new String(IoUtils.readFileAsString(tmp.getPath())));
}
Also used : FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

FileChannel (java.nio.channels.FileChannel)676 IOException (java.io.IOException)247 ByteBuffer (java.nio.ByteBuffer)215 File (java.io.File)199 FileInputStream (java.io.FileInputStream)177 FileOutputStream (java.io.FileOutputStream)167 RandomAccessFile (java.io.RandomAccessFile)151 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)82 Path (java.nio.file.Path)42 FileLock (java.nio.channels.FileLock)34 FileNotFoundException (java.io.FileNotFoundException)32 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 OutputStream (java.io.OutputStream)12 ReadableByteChannel (java.nio.channels.ReadableByteChannel)12 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9