aboutsummaryrefslogtreecommitdiff
path: root/java/com/xagasoft/gats/GatsInputStream.java
blob: 241701860b957f67d4a22105e05239aeb36d5dfa (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
package com.xagasoft.gats;

import java.io.InputStream;
import java.io.DataInputStream;

/**
 * Facilitates reading GatsObjects from an InputStream.  This doesn't really
 * inherit from InputStream, so maybe it would make more sense to call it
 * something else.  Use the readObject function to read an entire object from
 * the InputStream.
 * <p>
 * At the moment this class will require all data to be available in continuous
 * read operations from teh provided InputStream.  This means that only complete
 * packets can be read from files on the disk, or that if a socket is provided
 * it is in blocking or synchronous I/O mode.  In java, this should rarely be
 * an issue.
 * <p>
 * Each call to readObject returns a new GatsObject read from the InputStream or
 * null if nothing else could be read.  While reading, all zero bytes discovered
 * in between packets will be considered padding and will be ignored.
 * <p>
 * Like with the GatsOutputStream, there are generally many small reads
 * performed during a single readObject operation, so it is a good idea to
 * provide some sort of buffered input stream.
 *@see com.xagasoft.gats.GatsOutputStream
 */
public class GatsInputStream
{
	private InputStream is;
	private int iVer = 0;
	private int iSize;

	public GatsInputStream( InputStream is )
	{
		this.is = is;
	}

	/**
	 * Reads an object from the input and returns it.
	 */
	public GatsObject readObject() throws java.io.IOException
	{
		do
		{
			iVer = is.read();
		} while( iVer == 0 );

		switch( iVer )
		{
			case 1:
				DataInputStream dis = new DataInputStream( is );
				iSize = dis.readInt();
				return GatsObject.read( is );
		}

		return null;
	}
};