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