Java Byte Array to Object deserialize(byte[] bytes)

Here you can find the source of deserialize(byte[] bytes)

Description

deserialize

License

Apache License

Declaration

public static Object deserialize(byte[] bytes) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

public class Main {
    private static final int ZLIB_COMPRESSION = 1;

    public static Object deserialize(byte[] bytes) throws IOException {
        if (bytes.length < 2) {
            throw new IOException("Invalid bytes content");
        }/*w  w  w. ja v  a  2s.c o m*/
        InputStream in = new ByteArrayInputStream(bytes);
        if (bytes[0] == 0) {
            in.read(); // consume the marker;
            if (in.read() != ZLIB_COMPRESSION) {
                throw new IOException("Unknown compression type");
            }
            final Inflater inflater = new Inflater(true);
            in = new InflaterInputStream(in, inflater) {
                @Override
                public void close() throws IOException {
                    try {
                        super.close();
                    } finally {
                        inflater.end();
                    }
                }
            };
        }
        try (ObjectInputStream oin = new ObjectInputStream(in)) {
            try {
                return oin.readObject();
            } catch (ClassNotFoundException e) {
                throw new IOException("Exception while deserilaizing.", e);
            }
        }
    }
}

Related

  1. deserialize(byte[] bytes)
  2. deserialize(byte[] bytes)
  3. deserialize(byte[] bytes)
  4. deserialize(byte[] bytes)
  5. deserialize(byte[] bytes)
  6. deserialize(byte[] bytes)
  7. deserialize(byte[] bytes)
  8. deserialize(byte[] bytes)
  9. deserialize(byte[] bytes)