110
Java ObjectInputStream class
The objectinputstream class is mainly used to deserialize the primitive data and objects which are written by using ObjectOutputStream. ObjectInputStream can also be used to pass the objects between hosts by using a SocketStream. The objects which implement Serializable or Externalizable interface can only be read using ObjectInputStream. Consider the following line of code to read an object from the file which was previously written by using ObjectOutputStream.
Java ObjectInputStream class declaration
List of ObjectInputStream Methods
NO | Method | Description |
---|---|---|
1 | public int available() throws IOException | The available() method returns the number of bytes that can be read without blocking. |
2 | public void close() throws IOException | The close() method closes the input stream. It must be called to release any resource associated with the stream. |
3 | public void defaultReadObject() throws IOException, ClassNotFoundException | The default ReadObject() method reads the non-static and non-transient fields of the current class from this stream. i.e one cannot use defaultReadObject() to read the static fields. This may only be called from the readObject method of the class being desterilized. You cannot use defaultReadObject() directly. |
4 | public int read() throws IOException | The read() method of java.io.ObjectInputStream reads a byte of data. This method will block if no input is available. i.e., some data should be present to read in inputstream otherwise method will block. The actual number of bytes read or -1 is returned when the end of the stream is reached. |
5 | public int read(byte[] buf, int off, int len) throws IOException | The read() method read into an array of bytes. There should be some bytes present to be read otherwise the method will block. This method takes 3 parameters, a byte array buff into which the bytes are stored, starting offset off to start reading, Length of bytes len. |
6 | public boolean readBoolean() throws IOException | This method reads a Boolean from the stream. It returns true if the byte is non-zero otherwise false |
7 | public byte readByte() throws IOException | The readByte() method reads an 8-bit byte. |
8 | public char readChar() throws IOException | The readChar() method reads a 16-bit char. |
9 | Public double readDouble() throws IOException | |
10 | public float readFloat() throws IOException | The readFloat() method reads a 32-bit float. |
11 | Public int readInt() throws IOException | The readInt() method reads a 32-bit int. |
12 | public long readLong() throws IOException | The readLong() method reads a 64-bit float. |
13 | Public short readShort() throws IOException | The readShort() method read 16 bit short. |
14 | public String readLine() throws IOException | The readLine() method reads in a line that has been terminated by a n, r, rn or EOF. |
15 | public void readFully(byte[] buf) throws IOException public void readFully(byte[] buf, int off, int len) throws IOException | The readFully() method of ObjectInputStream class Reads bytes until all the bytes in objectinputstream are read. This method is overloaded as It takes 3 parameters, the buffer into which data is stored, from where to start reading, and how many bytes to read. |
16 | public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException | The readField() method of ObjectInputStream class reads the persistent fields from the stream and makes them available by name. |
17 | public final Object readObject() throws IOException, ClassNotFoundException | The readObject() method of ObjectInputStream class is used to read an object from objectinputstresm. |
18 | public String readUTF() throws IOException | The readUTF() method of ObjectInputStream class reads a String in modified UTF-8 format. It returns String. |
19 | public int readUnsignedShort() throws IOException | The readUnsignedShort() method of ObjectInputStream class reads an unsigned 16 bit short. |
20 | public int readUnsignedByte() throws IOException | The readUnsignedByte() method of ObjectInputStream class reads an unsigned 8-bit byte. |
21 | public int skipBytes(int len) throws IOException | The skipBytes() method of ObjectInputStream class skips bytes by the number passed as a parameter. |
22 | protected Object readObjectOverride() throws IOException, ClassNotFoundException | The readObjectOverride() method of ObjectInputStream class is called by the trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor. The subclass is expected to provide an override method with the modifier “final.” |
23 | public Object readUnshared() throws IOException, ClassNotFoundException | The readUnshared() method of ObjectInputStream class reads an “unshared” object from the ObjectInputStream. |
24 | public void registerValidation(ObjectInputValidation obj, int prio) throws NotActiveException, InvalidObjectException | The registerValidation() method of ObjectInputStream class registers an object to be validated before the graph is returned. |
25 | protected boolean enableResolveObject(boolean enable) throws SecurityException | The enableResolveObject () method of ObjectInputStream enables the stream to do replacement of objects read from the stream. When enabled, the resolveObject(java.lang.Object) method is called for every object being deserialized. |
26 | protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException | The resolveClass() method loads the local class equivalent of the specified stream class description. Subclasses may implement this method to allow classes to be fetched from an alternate source. |
27 | protected void readStreamHeader() throws IOException, StreamCorruptedException | The readStreamHeader() method of ObjectInputStream is provided to allow subclasses to read and verify their stream headers. It reads and verifies the magic number and version number. |
Java ObjectInputStream Class Example: read()
Output:
Hello TutorAspire No of bytes read : 6
Java ObjectInputStream Class Example: readShort()
Output:
1 2 6 6 7 3
Java ObjectInputStream Class Example: readFully()
Output:
welcom to TutorAspire
Next TopicJava ObjectInputStream