blob: ad77a7f4f3afaf72bbfa36876eaa131d03f71a6d (
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
|
/*
* Copyright (C) 2007-2013 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.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 the 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 iLastSize = 0;
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 );
iLastSize = dis.readInt();
return GatsObject.read( is );
}
return null;
}
/**
* Set after each call to readObject, this will tell you how many bytes
* were actually read in to build the object that was returned.
* @return Size in bytes of the most recently read packet.
*/
public int getLastReadPacketSize()
{
return iLastSize;
}
};
|