1 / 6

Efficient Buffer Management and File Handling in Java NIO

This lecture explores Java NIO's powerful buffer management, channels, memory mapping, and file locking. Developers will learn about pre-defined, performance-optimized buffers like ByteBuffer and CharBuffer, and how to work with different channel types, including FileChannel, SocketChannel, and DatagramChannel. The session also covers memory mapping with MappedByteBuffer and demonstrates file locking mechanisms in Java. A practical example using ReadPrimes illustrates reading prime numbers from a file using buffers and channels, enhancing understanding of these advanced features in Java I/O.

marin
Télécharger la présentation

Efficient Buffer Management and File Handling in Java NIO

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IO Lecture 5

  2. java.nio • New: • Buffer management • Channels • File locking • Memory mapping

  3. java.nio: Buffers • “Buffer” a well-known concept, but is often home brewed by developers. They are now predefined and performance-optimized. • ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, MappedByteBuffer, ShortBuffer

  4. java.nio: Channels • A flexible concept that includes any open connection to a program entity. • DatagramChannel: when working with datagram sockets (UDP). • SocketChannel: for use with TCP/IP sockets. • FileChannel: for reading, writing, mapping, manipulating files. • Pipe.SinkChannel/-SourceChannel: for use with the writable/readable end of a pipe.

  5. java.nio: Memory mapping • MappedByteBuffer • We may map the contents of a file into a region of memory. • File locking necessary

  6. import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.FileNotFoundException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ReadPrimes { public static void main(String[] args) { File aFile = new File("primes.bin"); FileInputStream inFile = null; try { inFile = new FileInputStream(aFile); } catch(FileNotFoundException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel inChannel = inFile.getChannel(); final int PRIMECOUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8*PRIMECOUNT); long[] primes = new long[PRIMECOUNT]; try { while(inChannel.read(buf) != -1) { ((ByteBuffer)(buf.flip())).asLongBuffer().get(primes); // List the primes read on the same line System.out.println(); for(long prime : primes) System.out.printf("%10d", prime); buf.clear(); // Clear the buffer for the next read } System.out.println("\nEOF reached."); inFile.close(); // Close the file and the channel } catch(IOException e) { e.printStackTrace(System.err); System.exit(1); } System.exit(0); } } New I/O Example (ReadPrimes) From the channel, read data and save to the buffer Channel Setup You also need to read the “PrimesToFile.java” which prints prime numbers to the file. Buffer Setup Java Programming

More Related