11
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(reg_be);
oos.flush();
oos.close();

InputStream is = new ByteArrayInputStream(baos.toByteArray());

This code convert Java Object to InputStream and how can I convert InputStream to an Object? I need to convert my Object to an InputStream then I pass it and I want to get my Object back.

1
  • 2
    ObjectInputStream seems REALLY obvious. Commented Jun 11, 2013 at 11:31

4 Answers 4

18

In try block you should write:

ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();

ObjectInputStream is initialized with another stream, e.g. BufferedInputStream or your input stream is.

Sign up to request clarification or add additional context in comments.

2 Comments

ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(reg_be); oos.flush(); oos.close(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); this code showing exception java.io.NotSerializableException: why?
Because the class of the object (reg_be) you are serializing/deserializing must implement Serializable interface.
1
ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();

2 Comments

ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(reg_be); oos.flush(); oos.close(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); this code showing exception java.io.NotSerializableException: why?
Because your class doesn't implement Serializable.
0

Try the following

ObjectInputStream  ois = new ObjectInputStream(is);
Object obj = ois .readObject();

5 Comments

ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(reg_be); oos.flush(); oos.close(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); this code showing exception java.io.NotSerializableException: why?
Why posting a completely same answer like previous two?
Does you class implement Serializable? It should or else you will get that exception. Class whose object you are transferring in your I/O streams.
@darijan did not see the previous two.
ok i have to serialize my object... ok ill do. thanks for your help
0
ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();

As mentioned by @darijan is working fine. But again we need to do try, catch block for that code, & for blank input stream it will give EOF (End Of File) related error.

So, I am converting it to a string. Then if the string is not empty or null, then only I am converting it to Object using ObjectMapper

Although it's not an efficient approach, I don't need to worry about try-catch, null handling also is done in a string instead of the input stream

String responseStr = IOUtils.toString(is, StandardCharsets.UTF_8.name());
Object object = null;
// is not null or whitespace consisted string
if (StringUtils.isNotBlank(response)) {
    object = getJsonFromString(response);
}


// below codes are already used in project (Util classes)
private Object getJsonFromString(String jsonStr) {
    if (StringUtils.isEmpty(jsonStr)) {
        return new LinkedHashMap<>();
    }
    ObjectMapper objectMapper = getObjectMapper();

    Map<Object, Object> obj = null;
    try {
        obj = objectMapper.readValue(jsonStr, new TypeReference<Map<Object, Object>>() {
        });
    } catch (IOException e) {
        LOGGER.error("Unable to parse JSON : {}",e)
    }
    return obj;
}
private ObjectMapper getObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    return objectMapper;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.