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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

The java.util.jar package includes two stream classes for reading files from JAR archives.<br />

These will also be discussed in Chapter 9.<br />

JarInputStream JarOutputStream<br />

The java.security package includes a couple of stream classes used for calculating<br />

message digests:<br />

DigestInputStream DigestOutputStream<br />

The <strong>Java</strong> Cryptography Extension (JCE) adds two classes for encryption and decryption:<br />

CipherInputStream CipherOutputStream<br />

These four streams will be discussed in Chapter 10.<br />

Finally, there are a few random stream classes hiding inside the sun packages—for example,<br />

sun.net.TelnetInputStream and sun.net.TelnetOutputStream . However, these are<br />

deliberately hidden from you and are generally presented as instances of<br />

java.io.InputStream or java.io.OutputStream only.<br />

1.2 Numeric Data<br />

Input streams read bytes and output streams write bytes. Readers read characters and writers<br />

write characters. Therefore, to understand input and output, you first need a solid<br />

understanding of how <strong>Java</strong> deals with bytes, integers, characters, and other primitive data<br />

types, and when and why one is converted into another. In many cases <strong>Java</strong>'s behavior is not<br />

obvious.<br />

1.2.1 Integer Data<br />

The fundamental integer data type in <strong>Java</strong> is the int, a four-byte, big-endian, two's<br />

complement integer. An int can take on all values between -2,147,483,648 and<br />

2,147,483,647. When you type a literal integer like 7, -8345, or 3000000000 in <strong>Java</strong> source<br />

code, the compiler treats that literal as an int. In the case of 3000000000 or similar numbers<br />

too large to fit in an int, the compiler emits an error message citing "Numeric overflow."<br />

longs are eight-byte, big-endian, two's complement integers with ranges from -<br />

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. long literals are indicated by<br />

suffixing the number with a lower- or uppercase L. An uppercase L is preferred because the<br />

lowercase l is too easily confused with the numeral 1 in most fonts. For example, 7L, -8345L,<br />

and 3000000000L are all 64-bit long literals.<br />

There are two more integer data types available in <strong>Java</strong>, the short and the byte. shorts are<br />

two-byte, big-endian, two's complement integers with ranges from -32,768 to 32,767. They're<br />

rarely used in <strong>Java</strong> and are included mainly for compatibility with C.<br />

bytes, however, are very much used in <strong>Java</strong>. In particular they're used in I/O. A byte is an<br />

eight-bit, two's complement integer that ranges from -128 to 127. Note that like all numeric<br />

17

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

Saved successfully!

Ooh no, something went wrong!