aboutsummaryrefslogtreecommitdiff
path: root/java/com/xagasoft/gats/GatsObject.java
blob: df489aa227460af857a957a1e5e910268e965ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * 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;
	}
};