Code Search for Developers
 
 
  

LiteResult.java from GridBlocks at Krugle


Show LiteResult.java syntax highlighted

/*
 * Copyright (c) 2004 
 * Helsinki Institute of Physics
 * see LICENSE file for details
 *
 * LiteResult.java
 * Created on Aug 19, 2003
 */

package fi.hip.gb.midlet.core;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import fi.hip.gb.midlet.util.StorageUtils;

/**
 * Minimal result container for one job. Contains the actual result and 
 * metadata about the result.
 * 
 * @author Juho Karppinen
 */
public class LiteResult {	
	/** result is all text */
	public static final int TEXT_TYPE = 0;
	/** result is image */
	public static final int IMAGE_TYPE = 1;
	/** result is audio */
	public static final int AUDIO_TYPE = 2;
	/** result is gps */
	public static final int GPS_TYPE = 2;
	
	/** name of the result file*/
	private String name;
	/** type of the data */
	private int type = -1;
	/** additional description about the result */
	private String description;
	/** size of the result */
	private int size;
	
	/** actual payload data, as object or byte array */
    private Object data;
	/** own record ID */
	private int recordID = -1;
	
	/** name of the record store for results */
	public final static String STORAGE_NAME = "results";
	
	/**
	 * Default empty constructor
	 */
	public LiteResult() {
	}
	
	/**
	 * Loads the object from the memory
	 * @param recID record ID
	 */
	public LiteResult(int recID) {
		this.recordID = recID;
		try {
			DataInputStream  dis = StorageUtils.read(STORAGE_NAME, this.recordID);
			deSerialize(dis);
			dis.close();
		} catch (Exception e) {
			System.out.println("Failed to read result from " + this.recordID
					+ " : " + e.getMessage());
		}
	}
	
	/**
	 * Constructs new storage for result. The type of result
	 * is determined based on file suffix.
	 * <p>
	 * Currenlty supported result types are:
	 * <ul>
	 * <li>.png and jpeg images
	 * <li>.wav audio
	 * <li>all other suffixes are handles as plain text
	 * </ul>
	 *
	 * @param resultName name of the job 
	 * @param type classname of the result
	 * @param size size of the result, used to give the size if no payload exists
	 * @param resultData actual payload
	 */
	public LiteResult(String resultName, String type, int size, byte[] resultData) {
		this.name = resultName;
		this.description = type;
		this.size = size;
		this.data = resultData;
		
		System.out.println("RESULT " + resultName 
		        + " size is " + resultData.length + " / " + this.size);
		
		String lowerCaseName = name.toLowerCase();
		if(lowerCaseName.endsWith(".png") 
		        || lowerCaseName.endsWith(".jpeg") 
		        || lowerCaseName.endsWith(".jpg"))
		    this.type = IMAGE_TYPE;
		else if(lowerCaseName.endsWith(".wav"))
		    this.type = AUDIO_TYPE;
		else
		    this.type = TEXT_TYPE;
	}
	
	/**
	 * Reads result content
	 * @return content of the result
	 */
	public String readContent() {
		return new String((byte[])this.data);
	}

	/**
	 * Reads result content as bytes
	 * @return content of the result in bytes
	 */
	public byte[] readBytes() {
		return (byte[])this.data;
	}
    
    /**
     * Reads the result as object.
     * @return result as object
     */
    public Object readObject() {
        return this.data;
    }
	
	/**
	 * Write the payload as string.
	 * @param data as string
	 */
	public void writeContent(String data) {
	    this.data = data.getBytes();
	}
	/**
	 * Write the payload as byte array
	 * @param data as bytes
	 */
	public void writeBytes(byte[] data) {
	    this.data = data;
	}
    
    /**
     * Write the payload as object.
     * @param object object
     */
    public void writeObject(Object object) {
        this.data = object;
    }
	
	/**
	 * Gets the name of the result.
	 * When loaded from the server, the name is filename on the disk.
	 * @return filename of the file
	 */
	public String getName() {
		return this.name;
	}
	
	/**
	 * Sets the name of the result. 
	 * When loaded from the server, the name is filename on the disk.
	 * @param resultName name for the result
	 */
	public void setName(String resultName) {
	    this.name = resultName;
	}

	/**
	 * Gets the type of result data
	 * @return type as integer number
	 */
	public int getType() {
		return this.type;
	}

	/**
	 * Sets the type of result data
	 * @param resultType as integer number
	 */
	public void setType(int resultType) {
	    this.type = resultType;
	}
	
	/**
	 * Gets additional description about the result
	 * @return addtional informations
	 */
	public String getDescription() {
		return this.description;
	}
    /**
     * Sets additional description about the result
     * @param description addtional informations
     */
    public void setDescription(String description) {
        this.description = description;
    }

	/**
	 * Gets the size of the result
	 * @return result size in bytes
	 */
	public int getSize() {
		return this.size;
	}
	
	/**
	 * Gets the size of the actual payload stored inside the object.
	 * @return result size in bytes
	 */
	public int getPayloadSize() {
		return ((byte[])this.data).length;
	}
	
	/**
	 * Gets the record ID
	 * @return positive id if result is saved on the memory
	 */
	public int getRecordID() {
		return this.recordID;
	}

	/**
	 * Stores the object into the record store
	 * @return record ID of the result on the record store
	 * @throws Exception if result cannot be save
	 */ 
	public int store() throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		this.serialize(dos);
		dos.close();
		this.recordID = StorageUtils.store(baos.toByteArray(), STORAGE_NAME);
		return this.recordID;
	}

	/**
	 * Serialize the container
	 * @param dos stream to write to
	 * @throws IOException if fields cannot be serialized
	 */
	protected void serialize(DataOutputStream dos) throws IOException {
		dos.writeUTF(this.name);
		dos.writeUTF(this.description);
		dos.writeInt(this.type);
		dos.writeInt(((byte[])this.data).length);
		dos.write(((byte[])this.data), 0, ((byte[])this.data).length);
	}

	/**
	 * Deserialize the container from inputstream
	 * @param dis inputstream to read
	 * @throws IOException if failed to serialize from the stream
	 */
	protected void deSerialize(DataInputStream dis) throws IOException {
	    this.name = dis.readUTF();
	    this.description = dis.readUTF();
	    this.type = dis.readInt();
		int length = dis.readInt();
		if(length > 0) {
		    this.data = new byte[length];
			dis.read(((byte[])this.data), 0, length);
		}
	}
	
	/**
	 * Prints some debugging information
	 * @return String representation of the object
	 */
	public String toString() {
		return
			"name: " + this.name + "\n" +
			"description: " + this.description + "\n" +
			"type: " + this.type + "\n" +
			"data: " + this.data;
	}
}



See more files for this project here

GridBlocks

GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.

Project homepage: http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other

  LiteDescription.java
  LiteResult.java
  LiteStatus.java
  LiteStorage.java