1 / 6

Lecture 5

IO. Lecture 5. java.nio. New: Buffer management Channels File locking Memory mapping. java.nio: Buffers. “Buffer” a well-known concept, but is often home brewed by developers. They are now predefined and performance-optimized.

marin
Download Presentation

Lecture 5

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