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"); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access