[ Pobierz całość w formacie PDF ]
.size() Returns the current size of the buffer.toByteArray() Returns a copy of the input data.toString() Converts input bytes to a string.toString(int) Converts input bytes to a string, sets the selected byte s first 8 bits of a 16 bitUnicode to hibyte.write(int) Writes n number of bytes to the buffer.write(byte[], int, int) Writes an array of bytes of a specified size, starting at a specified index position.writeTo(OutputStream) Writes the buffered information to another stream.330 Chapter 12import java.io.*;// Reads from a filepublic class Byte2String extends Object {Byte2String(String s) {byte[] anArrayOBytes;&//fills the anArrayOBytes with data&try {// Creates an instanceof the class InputStream named byteDataIn// byteDataIn receives the stream from the ByteArrayInputStream// anArrayOBytesInputStream byteDataIn = new ByteArrayInputStream(anArrayOBytes);}catch(IOException e) {}&// perform some process with the stream}// Where execution begins in a stand-alone executablepublic static void main(String args[]) {new Byte2String(args[0]);}}DataInputStream and DataOutputStream ClassesAll methods defined in these classes are actually declared in an interface namedDataInput.To declare and create a DataInputStream or DataOutputStreamobject you would use statements like the following:DataInputStream aStream = new DataInputStream(getStreamFromASource());DataOutputStream aStream = new DataOutputStream(getStreamToASource());Notice in both declarations the need to declare the class type, DataInputStreamor DataOutputStream, instead of type InputStream or OutputStream.Themethods getStreamFromSource() and getStreamToASource() can be any methodsthat return a stream.Once the stream is passed to the DataInputStream orDataOutputStream object, the methods declared in the interface can be appliedto the stream.Table 12.7 presents the key methods defined in DataInputStreamand Table 12.8 presents the methods defined in DataOutputStream.Streams and File I/O 331Table 12.7 Key Methods Defined in the DataInputStream ClassMethod Descriptionread(byte[]) Reads an array of bytes from the stream.read(byte[], int, int) Reads a specified number of bytes from the stream and places them in an arraystarting at a specified index position.readBoolean() Reads a boolean from the stream.readByte() Reads a 8-bit byte from the stream.readChar() Reads a 16-bit character from the stream.readDouble() Reads a 64-bit double from the stream.readFloat() Reads a 32-bit float from the stream.readFully(byte[]) Reads bytes from the stream, blocking until all bytes are read.readFully(byte[], int, int) Reads bytes from the stream, blocking until all bytes are read.The starting pointto begin reading and the maximum number of bytes to read are passed asparameters.readInt() Reads a 32-bit integer from the stream.readLine() Reads a line from the stream until an \n,\r,\n\r \, or EOF is reached.readLong() Reads a 64-bit long from the stream.readShort() Reads a 16-bit short from the stream.readUTF() Reads a UTF formatted string from the stream.readUTF(DataInput) Reads a UTF formatted string from a specific stream.readUnsignedByte() Reads an unsigned 8-bit byte from the stream.readUnsignedShort() Reads an unsigned 8-bit short from the stream.skipBytes(int) Skips a designated number of bytes in the stream, blocking until finished.Table 12.8 Key Methods Defined in the DataOutputStream ClassMethod Descriptionflush() Clears the stream, forcing any buffered bytes to be written out.size() Returns the number of bytes in the stream.write(int) Writes n number of bytes to the stream.write(byte[], int, int) Writes an array of bytes of a specified size, starting at a specified index position.writeBoolean(boolean) Writes a boolean to the stream.writeByte(int) Writes an 8-bit byte to the stream.writeBytes(String) Writes a string of bytes to the stream.continued332 Chapter 12Table 12.8 Key Methods Defined in the DataOutputStream Class (Continued)Method DescriptionwriteChar(int) Writes a 16-bit character to the stream.writeChars(String) Writes a string of chars to the stream.writeDouble(double) Writes a 64-bit double to the stream.writeFloat(float) Writes a 32-bit float to the stream.writeInt(int) Writes a 32-bit integer to the stream.writeLong(long) Writes a 64-bit long to the stream.writeShort(int) Writes a 16-bit short to the stream.writeUTF(String) Writes a UTF formatted string to the stream.Let s revisit our example of the client application once more to demonstrate theuse of a DataInputStream:import java.io.*;import java.net.*;import java.awt.*;public class mrClient extends Frame {mrClient() {super("Client Application");TextArea clientScreen = new TextArea("mrClient's Screen:\n", 10, 40);add("Center", clientScreen);pack();show();Socket mrClient = null;InputStream rawDataIn = null;try {mrClient = new Socket( InetAddress.getLocalHost(), 10 );rawDataIn = mrClient.getInputStream();// reads in the stream for the InputStream rawDataIn and pipes// it to DataInDataInputStream DataIn = new DataInputStream(rawDataIn);// the array of bytes is then read from the streamclientScreen.appendText( "mrClient receives - " +DataIn
[ Pobierz całość w formacie PDF ]