Why is this weird behavior of ObjectOutputStream and ObjectInputStream throws an EOFException?

I wrote custom serializing / de-serializing logic to save some data, since Java default serialization turned out to be both time and memory expensive. For this, I wrote methods readObject(ObjectInput in)and writeObject(ObjectOutput out)for the class (es) that need to be preserved. However, I noticed that if I do not use the out.writeObject(obj)in method writeObject(ObjectOutput out), it always throws EOFException.

Consider the following example:

Data.java

public class Data implements BaseData {

private String messageUID;
private String rawData;
private String data;
private Long type;
private Boolean processed = false;
private String processedMessage;
private String processedDetaildMessage;

// getter setter

public void readObject(ObjectInput in) throws IOException, ClassNotFoundException {
    messageUID = in.readUTF();
    rawData = in.readUTF();
    data = in.readUTF();
    type = in.readLong();
    processed = in.readBoolean();
    if (processed) {
        processedMessage = in.readUTF();
        processedDetaildMessage = in.readUTF();
    }
}

public void writeObject(ObjectOutput out) throws IOException {
    out.writeUTF(messageUID);
    out.writeUTF(rawData);
    out.writeUTF(data);
    out.writeLong(type);
    out.writeBoolean(processed);
    if (processed) {
        out.writeUTF(processedMessage);
        String tempDetailsMessage[] = processedDetaildMessage.split(" more");
        out.writeUTF(tempDetailsMessage[tempDetailsMessage.length - 1]);
    }
}

However, whenever I use the code above, the stream outalways skips some information at the end (from the field processedDetaildMessage), and when I EOFExceptionread the form in, I get stacktrace below (line Data.java 216 processedDetaildMessage = in.readUTF());

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.readByte(ObjectInputStream.java:2766)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTFChar(ObjectInputStream.java:3158)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3055)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2864)
    at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1072)
    at com.smartstream.common.Data.readObject(Data.java:216)
    at com.smartstream.common.PerformanceTest.getObjectFromBytes(PerformanceTest.java:168)
    at com.smartstream.common.PerformanceTest.access$0(PerformanceTest.java:160)
    at com.smartstream.common.PerformanceTest$1.mapRow(PerformanceTest.java:119)
    at com.smartstream.common.PerformanceTest$1.mapRow(PerformanceTest.java:1)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:651)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:639)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:668)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:676)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:731)
    at com.smartstream.common.PerformanceTest.readFromDb(PerformanceTest.java:109)
    at com.smartstream.common.PerformanceTest.main(PerformanceTest.java:66)

/ , . out.writeByte(-1), out.writeInt(-1), out.writeLong(2342343l), out.writeUTF("END_OF_STREAM"), . , out.writeObject(new String("END_OF_STREAM")), . - , , writeObject(). , / ;

private byte[] getObjectAsBytes(Data data) {
    byte[] byteArray = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        // Use this for java default serialization
        // oos.writeObject(data);
        data.writeObject(oos);
        byteArray = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (oos != null) {
            try {
                oos.flush();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return byteArray;
}

private Data getObjectFromBytes(byte[] byteArray) {
    Data data = new Data();
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
        bais = new ByteArrayInputStream(byteArray);
        ois = new ObjectInputStream(bais);
        // Use this for java default serialization
        // data = (Data) ois.readObject();
        data.readObject(ois);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return data;
}

- , , ;

( EOFException ) ( stacktrace , stacktrace processedDetailedMessage)

¬í---z-------3507319347632941385----FEEDER-----1437052314954 ---This is a random string---N---þ%J---!this is message of processed dataÛ
Caused by: java.sql.SQLException: ORA-01691: unable to extend lob segment TLM_DBO.SYS_LOB0000076335C00008$$ by 8192 in tablespace WIN_SL_TABLE
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1008)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
at oracle.jdbc.driver.OraclePre

writeObject

¬í---z-------3507319347632941385----FEEDER-----1437052314954 ---This is a random string---N---þ%J---!this is message of processed dataÛ
Caused by: java.sql.SQLException: ORA-01691: unable to extend lob segment TLM_DBO.SYS_LOB0000076335C00008$$ by 8192 in tablespace WIN_SL_TABLE
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1008)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
at oracle.jdbc.driver.OraclePrz-----NeparedStatement.execute(OraclePreparedStatement.java:3550)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.pmiExecute(WSJdbcPreparedStatement.java:975)
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.execute(WSJdbcPreparedStatement.java:642)
at com.smartstream.control.engine.config.dao.jdbc.ProcessExecutionAuditDetailDao$1.doInPreparedStatement(ProcessExecutionAuditDetailDao.java:115)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
... 23t 
END_OF_STREAM

PS ----

+4
2

, - , ObjectOutputStream. getObjectAsBytes(Data) byteArray = bos.toByteArray(); finally, . , ( Java 7 +):

private byte[] getObjectAsBytes(Data data) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
        data.writeObject(oos);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bos.toByteArray();
}

, EOFException.

writeObject, writeObject , , OutputStream, ByteArrayOutputStream .

+3

- writeObject write*, .. writeUTF. writeObject , , OutputStream, , flush OutputStream. , byteArray , . , writeObject;

public void writeObject(ObjectOutput out) throws IOException {
    out.writeUTF(messageUID);
    out.writeUTF(rawData);
    out.writeUTF(data);
    out.writeLong(type);
    out.writeBoolean(processed);
    if (processed) {
        out.writeUTF(processedMessage);
        String tempDetailsMessage[] = processedDetaildMessage.split(" more");
        out.writeObject(tempDetailsMessage[tempDetailsMessage.length - 1]);
    }
}
-1

Source: https://habr.com/ru/post/1598228/


All Articles