23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

public void writeInt(int i) throws <strong>IO</strong>Exception {<br />

}<br />

out.write(i & 0xFF);<br />

out.write((i >>> 8) & 0xFF);<br />

out.write((i >>> 16) & 0xFF);<br />

// What happens if another thread preempts here?<br />

out.write((i >>> 24) & 0xFF);<br />

written += 4;<br />

<strong>Java</strong> I/O<br />

Suppose a second thread preempts the running thread where indicated in the previous code<br />

and writes unrelated data onto the output stream. The entire stream can be corrupted, because<br />

the bytes of the int are separated. The same synchronization tricks work here as well.<br />

However, all the problems I've noted here are shared by DataInputStream and<br />

DataOutputStream. Similar problems crop up in other filter stream classes. This leads to the<br />

following general principle for thread-safe programming:<br />

Never allow two threads to share a stream.<br />

The principle is most obvious for filter streams, but it applies to regular streams as well.<br />

Although writing or reading a single byte can be treated as an atomic operation, many<br />

programs will not be happy to read and write individual bytes. They'll want to read or write a<br />

particular group of bytes and will not react well to being interrupted.<br />

7.10 File Viewer, Part 3<br />

In Chapter 4, I introduced a FileDumper program that could print the raw bytes of a file in<br />

ASCII, hexadecimal, or decimal. In this chapter, I'm going to expand that program so that it<br />

can interpret the file as containing binary numbers of varying widths. In particular I'm going<br />

to make it possible to dump a file as shorts, unsigned shorts, ints, longs, floats, and<br />

doubles. Integer types may be either big-endian or little-endian. The main class,<br />

FileDumper3, is shown in Example 7.10. As in Chapter 4, this program reads a series of<br />

filenames and arguments from the command line in the main() method. Each filename is<br />

passed to a method that opens a file input stream from the file. Depending on the commandline<br />

arguments, a particular subclass of DumpFilter from Chapter 6 is selected and chained to<br />

the input stream. Finally, the StreamCopier.copy() method pours data from the input stream<br />

onto System.out.<br />

Example 7.10. The FileDumper3 Class<br />

import java.io.*;<br />

import com.macfaq.io.*;<br />

public class FileDumper3 {<br />

public static final int ASC = 0;<br />

public static final int DEC = 1;<br />

public static final int HEX = 2;<br />

public static final int SHORT = 3;<br />

public static final int INT = 4;<br />

public static final int LONG = 5;<br />

public static final int FLOAT = 6;<br />

public static final int DOUBLE = 7;<br />

124

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!