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.

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

The output buffer size is the number of bytes the driver can store for the output stream before<br />

it can write to the port. This is important, because it's easy for a fast program to write data<br />

faster than the port can send it out. Buffer overruns are a common problem, especially on<br />

older PCs with slower serial ports.<br />

Each of these properties has four methods: one enables the property, one disables it, one<br />

checks whether the property is enabled, and one returns the current value. For instance, the<br />

receive threshold is adjusted by these four methods:<br />

public abstract void enableReceiveThreshold(int size)<br />

throws UnsupportedCommOperationException<br />

public abstract void disableReceiveThreshold()<br />

public abstract boolean isReceiveThresholdEnabled()<br />

public abstract int getReceiveThreshold()<br />

The other three properties follow the same naming conventions. These four methods adjust<br />

the receive time-out:<br />

public abstract void enableReceiveTimeout(int rcvTimeout)<br />

throws UnsupportedCommOperationException<br />

public abstract void disableReceiveTimeout()<br />

public abstract boolean isReceiveTimeoutEnabled()<br />

public abstract int getReceiveTimeout()<br />

These four methods adjust the receive framing property:<br />

public abstract void enableReceiveFraming(int framingByte)<br />

throws UnsupportedCommOperationException<br />

public abstract void disableReceiveFraming()<br />

public abstract boolean isReceiveFramingEnabled()<br />

public abstract int getReceiveFramingByte()<br />

These four methods adjust the input and output buffer sizes:<br />

public abstract void setInputBufferSize(int size)<br />

public abstract int getInputBufferSize()<br />

public abstract void setOutputBufferSize(int size)<br />

public abstract int getOutputBufferSize()<br />

All drivers must support input and output buffers, so there are no isInputBufferEnabled()<br />

or disableOutputBuffer() methods. However, other than the input and output buffer sizes,<br />

drivers are not required to support these properties. If a driver does not support the given<br />

property, then enabling it will throw an UnsupportedCommOperationException. You can<br />

determine whether or not a driver supports a property by trying to enable it and seeing<br />

whether or not an exception is thrown. Example 17.7 uses this scheme to test the properties<br />

for the ports of the host system.<br />

Example 17.7. PortTester<br />

import javax.comm.*;<br />

import java.util.*;<br />

public class PortTester {<br />

441

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

Saved successfully!

Ooh no, something went wrong!