A Mapped I/O HTTP Server
The final section of this chapter presents the code to use the new mapped I/O facility in a socket server. As a refresher, here is a program that uses channel I/O to duplicate a file:
import java.io.*;import java.nio.*;import java.nio.channels.*;import java.net.*;class DupFile { void copyThruChannel(String fname) throws Exception { File f = new File(fname); FileInputStream fin = new FileInputStream(f); int len = (int) f.length(); FileChannel fc = fin.getChannel(); System.out.println("allocating buff"); ByteBuffer myBB = ByteBuffer.allocate(len); int bytesRead = fc.read(myBB); myBB.flip(); System.out.println("getting fout channel"); FileOutputStream fos = new FileOutputStream(fname+".copy"); ...
Get Just Java™ 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.