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.

}<br />

public int available() throws <strong>IO</strong>Exception {<br />

return buf.length - index;<br />

}<br />

public long skip(long bytesToSkip) throws <strong>IO</strong>Exception {<br />

}<br />

long bytesSkipped = 0;<br />

for (; bytesSkipped < bytesToSkip; bytesSkipped++) {<br />

int c = this.read();<br />

if (c == -1) break;<br />

}<br />

return bytesSkipped;<br />

public synchronized void mark(int readlimit) {}<br />

public synchronized void reset() throws <strong>IO</strong>Exception {<br />

throw new <strong>IO</strong>Exception("marking not supported");<br />

}<br />

public boolean markSupported() {<br />

return false;<br />

}<br />

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

The FilterInputStream class tacitly assumes that the number of bytes of input read from<br />

the underlying stream is the same as the number of bytes read from the filter stream. That's<br />

not always true, as is the case here. For instance, the HexFilter will provide three bytes of<br />

data for every byte read from the underlying stream. The DecimalFilter will provide four.<br />

Therefore, we also have to override skip() and available(). The skip() method simply<br />

reads as many bytes as possible, then returns. The available() method simply returns the<br />

number of bytes remaining in the buffer. For the uses we're putting these classes to, these<br />

methods aren't all that important, so I haven't bothered to provide optimal implementations.<br />

You can do better in subclasses, if you like.<br />

The same problem applies to the mark() and reset() methods. These will mark and reset the<br />

underlying stream, but what's really desired is to mark and reset this stream. The easiest<br />

solution here is to deliberately not support marking and resetting. Subclasses can override this<br />

if it seems important, or you can simply chain this stream to a buffered stream. However, the<br />

buffered stream must follow the dump filter in the chain rather than precede it.<br />

Concrete subclasses need only to implement a constructor or two and fill(). Example 6.10<br />

shows the DecimalFilter class. Example 6.11 shows the HexFilter class. These two<br />

classes are very similar. Each implements fill() and overrides available() (the latter<br />

mainly because it's straightforward to do). The algorithms for converting bytes to decimal and<br />

hexadecimal strings used by the fill() methods are essentially the same as used by the<br />

dumpDecimal() and dumpHex() methods back in Chapter 4's FileDumper program.<br />

Example 6.10. DecimalFilter<br />

package com.macfaq.io;<br />

import java.io.*;<br />

91

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

Saved successfully!

Ooh no, something went wrong!