Java ByteBuffer Serialize deserializeFromByteBufferNoHeader(ByteBuffer bytes)

Here you can find the source of deserializeFromByteBufferNoHeader(ByteBuffer bytes)

Description

deserialize From Byte Buffer No Header

License

Apache License

Declaration

public static <T> T deserializeFromByteBufferNoHeader(ByteBuffer bytes)
            throws IOException 

Method Source Code

//package com.java2s;
/*//from  ww  w . j  av  a2s. c  o m
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.nio.ByteBuffer;

public class Main {
    public static <T> T deserializeFromByteBufferNoHeader(ByteBuffer bytes)
            throws IOException {
        ObjectInputStream in = new ObjectInputStream(newInputStream(bytes)) {
            @Override
            protected void readStreamHeader() throws IOException {
                // do nothing
            }
        };
        try {
            T value = deserializeFromStream(in);
            if (in.read() != -1) {
                throw new IOException("Trailing bytes in " + bytes
                        + " after reading " + value);
            }
            return value;
        } finally {
            in.close();
        }
    }

    private static InputStream newInputStream(final ByteBuffer buf) {
        return new InputStream() {
            @Override
            public int read() {
                if (!buf.hasRemaining()) {
                    return -1;
                }
                return buf.get();
            }

            @Override
            public int read(byte[] bytes, int off, int len) {
                if (!buf.hasRemaining()) {
                    return -1;
                }
                int toRead = Math.min(len, buf.remaining());
                buf.get(bytes, off, toRead);
                return toRead;
            }
        };
    }

    public static <T> T deserializeFromStream(ObjectInputStream in)
            throws IOException {
        try {
            @SuppressWarnings("unchecked")
            T obj = (T) in.readObject();
            return obj;
        } catch (ClassNotFoundException e) {
            throw new IOException("Deserialization error", e);
        }
    }
}

Related

  1. deserialize(ByteBuffer b)
  2. deserialize(ByteBuffer byteBuffer)
  3. deserializeBoolean(ByteBuffer buf)
  4. deserializeObject(ByteBuffer obj)
  5. serializeBigDecimal(BigDecimal bd, ByteBuffer buf)
  6. serializeNull(ByteBuffer buf)
  7. serializeString(final String value, ByteBuffer buffer)