/* * Copyright (C) 2007-2012 Xagasoft, All rights reserved. * * This file is part of the libgats library and is released under the * terms of the license contained in the file LICENSE. */ package com.xagasoft.gats; import java.io.InputStream; import java.io.OutputStream; /** * The abstract base class of all Gats storage classes. You probably don't * need to worry about these functions at all, maybe getType. The IO functions * in this class shouldn't really be used since they won't contain the proper * packet header info. See com.xagasoft.gats.GatsOutputStream and * com.xagasoft.gats.GatsInputStream for that. */ public abstract class GatsObject { public final static int INTEGER = 1; public final static int FLOAT = 2; public final static int STRING = 3; public final static int LIST = 4; public final static int DICTIONARY = 5; public final static int BOOLEAN = 6; public final static int NULL = 7; /** * Gets the type of the current object, type can be one of INTEGER, FLOAT, * STRING, LIST, DICTIONARY, or BOOLEAN. */ public abstract int getType(); /** * Read an object from the given input stream, with a particular type, this * function is used internally. */ abstract void read( InputStream is, char cType ) throws java.io.IOException; /** * Write the current object to the output stream. */ abstract void write( OutputStream os ) throws java.io.IOException; /** * Static function that returns a new object deserialized from an input * stream. This still doesn't take advantage of packet data, so you * probably shouldn't use this yourself. */ static GatsObject read( InputStream is ) throws java.io.IOException { int b = is.read(); char type = (char)b; GatsObject goRet = null; switch( type ) { case 'i': goRet = new GatsInteger(); break; case 's': goRet = new GatsString(); break; case '0': case '1': goRet = new GatsBoolean(); break; case 'l': goRet = new GatsList(); break; case 'd': goRet = new GatsDictionary(); break; case 'f': case 'F': goRet = new GatsFloat(); break; case 'n': goRet = new GatsNull(); break; case 'e': return null; default: throw new java.io.IOException("Invalid gats type discovered: " + type + ", (" + b + ")" ); } goRet.read( is, type ); return goRet; } };