p. 162: The caption for Example 6-7, should be "Determining whether an IP address is v4 or v6".
p. 164: In the fifth paragraph change "Organization multicast addresses begin with FF05 or FF15" to "Site-wide multicast addresses begin with FF05 or FF15".
p. 165: In the caption for Example 6-8, change "(Java 1.4 only)" to "(Java 1.4 and later)"
p. 171: In the first code fragment on the page, change
NetworkInterface.getByName(local)
to
NetworkInterface.getByInetAddress(local)
.
p. 180: There are three errors in Example 6-13. First, change
new BufferedReader
to
new SafeBufferedReader
. Then in the
main()
method, change
e.printStackTrace()
to
ex.printStackTrace()
. Finally,
several lines from the processLogFile()
method
were omitted. The complete, corrected program is:
import java.io.*;
import java.util.*;
import com.macfaq.io.SafeBufferedReader;
public class PooledWeblog {
private BufferedReader in;
private BufferedWriter out;
private int numberOfThreads;
private List entries = Collections.synchronizedList(new LinkedList());
private boolean finished = false;
private int test = 0;
public PooledWeblog(InputStream in, OutputStream out, int numberOfThreads) {
this.in = new SafeBufferedReader(new InputStreamReader(in));
this.out = new BufferedWriter(new OutputStreamWriter(out));
this.numberOfThreads = numberOfThreads;
}
public boolean isFinished() {
return this.finished;
}
public int getNumberOfThreads() {
return numberOfThreads;
}
public void processLogFile() {
for (int i = 0; i < numberOfThreads; i++) {
Thread t = new LookupThread(entries, this);
t.start();
}
try {
String entry = in.readLine();
while (entry != null) {
if (entries.size() > numberOfThreads) {
try {
Thread.sleep((long) (1000.0/numberOfThreads));
}
catch (InterruptedException e) {}
continue;
}
synchronized (entries) {
entries.add(0, entry);
entries.notifyAll();
}
entry = in.readLine();
Thread.yield();
} // end while
}
catch (IOException e) {
System.out.println("Exception: " + e);
}
this.finished = true;
// finish any threads that are still waiting
synchronized (entries) {
entries.notifyAll();
}
}
public void log(String entry) throws IOException {
out.write(entry + System.getProperty("line.separator", "\r\n"));
out.flush();
}
public static void main(String[] args) {
try {
PooledWeblog tw = new PooledWeblog(new FileInputStream(args[0]),
System.out, 100);
tw.processLogFile();
}
catch (FileNotFoundException e) {
System.err.println("Usage: java PooledWeblog logfile_name");
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Usage: java PooledWeblog logfile_name");
}
catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
}
} // end main
}