Skip to content

Instantly share code, notes, and snippets.

@rbe
Created August 20, 2012 17:11
Show Gist options
  • Select an option

  • Save rbe/3405897 to your computer and use it in GitHub Desktop.

Select an option

Save rbe/3405897 to your computer and use it in GitHub Desktop.
Java 7 NIO Buffer Test
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class NioBufferTest {
public static void main(String[] args) throws Exception {
Path path = Paths.get(new URI("file://test.txt"));
ByteBuffer buffer = ByteBuffer.allocate(4 * 1024 * 1024); // 4 MB buffer
SeekableByteChannel b = Files.newByteChannel(path, StandardOpenOption.READ);
int read = 0;
int _readOps = 0;
while ((read = b.read(buffer)) > -1) {
_readOps++;
System.out.println("read=" + read);
buffer.clear();
}
System.out.println("readops=" + _readOps);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment