diff options
Diffstat (limited to 'java/com/xagasoft/gats/Integer.java')
-rw-r--r-- | java/com/xagasoft/gats/Integer.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/java/com/xagasoft/gats/Integer.java b/java/com/xagasoft/gats/Integer.java new file mode 100644 index 0000000..7da12d4 --- /dev/null +++ b/java/com/xagasoft/gats/Integer.java | |||
@@ -0,0 +1,71 @@ | |||
1 | package com.xagasoft.gats; | ||
2 | |||
3 | import java.io.OutputStream; | ||
4 | import java.io.InputStream; | ||
5 | |||
6 | public class Integer extends Type | ||
7 | { | ||
8 | public int getType() | ||
9 | { | ||
10 | return Type.INTEGER; | ||
11 | }; | ||
12 | |||
13 | /** | ||
14 | * Possible TODO: have this return a Number, and construct either a Long | ||
15 | * or BigInteger when appropriate. | ||
16 | */ | ||
17 | public static long readPackedInt( InputStream is ) | ||
18 | throws java.io.IOException | ||
19 | { | ||
20 | int b; | ||
21 | long rOut = 0; | ||
22 | boolean bNeg; | ||
23 | |||
24 | b = is.read(); | ||
25 | bNeg = (b&0x40) == 0x40; | ||
26 | rOut |= b&0x3F; | ||
27 | int c = 0; | ||
28 | while( (b&0x80) == 0x80 ) | ||
29 | { | ||
30 | b = is.read(); | ||
31 | rOut |= (long)(b&0x7F) << (6+7*(c++)); | ||
32 | } | ||
33 | if( bNeg ) | ||
34 | return -rOut; | ||
35 | return rOut; | ||
36 | } | ||
37 | |||
38 | public static void writePackedInt( OutputStream os, long iIn ) | ||
39 | throws java.io.IOException | ||
40 | { | ||
41 | int b; | ||
42 | |||
43 | if( iIn < 0 ) | ||
44 | { | ||
45 | iIn = -iIn; | ||
46 | b = (int)(iIn&0x3F); | ||
47 | if( iIn > b ) | ||
48 | b |= 0x80 | 0x40; | ||
49 | else | ||
50 | b |= 0x40; | ||
51 | } | ||
52 | else | ||
53 | { | ||
54 | b = (int)(iIn&0x3F); | ||
55 | if( iIn > b ) | ||
56 | b |= 0x80; | ||
57 | } | ||
58 | os.write( b ); | ||
59 | iIn = iIn >> 6; | ||
60 | |||
61 | while( iIn > 0 ) | ||
62 | { | ||
63 | b = (int)(iIn&0x7F); | ||
64 | if( iIn > b ) | ||
65 | b |= 0x80; | ||
66 | os.write( b ); | ||
67 | iIn = iIn >> 7; | ||
68 | } | ||
69 | } | ||
70 | }; | ||
71 | |||