package simplequeue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;

public class SimpleQueueItem {
	
	private String item;
	
	public SimpleQueueItem(String item) {
		this.item = item;
	}
		
	public SimpleQueueItem(byte[] data) {
			DataInput in = new DataInputStream(new ByteArrayInputStream(data));
			try {
				this.item = in.readUTF();
			} catch (IOException e) {
				// cannot really happen since we're de-serializing from a byte[]
				throw new RuntimeException(e);
			}
	}
		
		public byte[] serialize() {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutput out = new DataOutputStream(baos);
			try {
				out.writeUTF(this.item);
			} catch (IOException e) {
				// cannot really happen since we're serializing to a byte[]
				throw new RuntimeException(e);
			}
			return baos.toByteArray();
		}
	}
