All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.thorbenkuck.network.encoding.JavaObjectDecoder Maven / Gradle / Ivy

The newest version!
package com.github.thorbenkuck.network.encoding;

import com.github.thorbenkuck.network.exceptions.FailedDecodingException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.util.Collections;
import java.util.List;

public class JavaObjectDecoder implements ObjectDecoder {
	@Override
	public List apply(byte[] bytes) throws FailedDecodingException {
		try (ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes);
			 ObjectInput objectInput = new ObjectInputStream(byteInput)) {
			return Collections.singletonList(objectInput.readObject());
		} catch (IOException | ClassNotFoundException e) {
			throw new FailedDecodingException(e);
		}
	}

	@Override
	public String toString() {
		return "JavaObjectDecoder{}";
	}
}