diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2011-04-21 17:40:13 +0000 | 
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2011-04-21 17:40:13 +0000 | 
| commit | 4fb4a056c52d3a96b6ef9a4ceb5ce39fc716a9aa (patch) | |
| tree | 138b749f3e209d61a63abd1258947850404ca87d /java/com/xagasoft/gats/GatsInteger.java | |
| parent | 49216a230c331791b06df1527c3245c88e991c20 (diff) | |
| download | libgats-4fb4a056c52d3a96b6ef9a4ceb5ce39fc716a9aa.tar.gz libgats-4fb4a056c52d3a96b6ef9a4ceb5ce39fc716a9aa.tar.bz2 libgats-4fb4a056c52d3a96b6ef9a4ceb5ce39fc716a9aa.tar.xz libgats-4fb4a056c52d3a96b6ef9a4ceb5ce39fc716a9aa.zip | |
It...builds?
Diffstat (limited to 'java/com/xagasoft/gats/GatsInteger.java')
| -rw-r--r-- | java/com/xagasoft/gats/GatsInteger.java | 102 | 
1 files changed, 102 insertions, 0 deletions
| diff --git a/java/com/xagasoft/gats/GatsInteger.java b/java/com/xagasoft/gats/GatsInteger.java new file mode 100644 index 0000000..1093ab2 --- /dev/null +++ b/java/com/xagasoft/gats/GatsInteger.java | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | package com.xagasoft.gats; | ||
| 2 | |||
| 3 | import java.io.OutputStream; | ||
| 4 | import java.io.InputStream; | ||
| 5 | |||
| 6 | public class GatsInteger extends GatsObject | ||
| 7 | { | ||
| 8 | private long iValue = 0; | ||
| 9 | |||
| 10 | public GatsInteger() | ||
| 11 | { | ||
| 12 | } | ||
| 13 | |||
| 14 | public GatsInteger( long iValue ) | ||
| 15 | { | ||
| 16 | this.iValue = iValue; | ||
| 17 | } | ||
| 18 | |||
| 19 | public long getValue() | ||
| 20 | { | ||
| 21 | return iValue; | ||
| 22 | } | ||
| 23 | |||
| 24 | public void setValue( long iValue ) | ||
| 25 | { | ||
| 26 | this.iValue = iValue; | ||
| 27 | } | ||
| 28 | |||
| 29 | public int getType() | ||
| 30 | { | ||
| 31 | return GatsObject.INTEGER; | ||
| 32 | }; | ||
| 33 | |||
| 34 | public void read( InputStream is, char cType ) throws java.io.IOException | ||
| 35 | |||
| 36 | { | ||
| 37 | iValue = readPackedInt( is ); | ||
| 38 | } | ||
| 39 | |||
| 40 | public void write( OutputStream os ) throws java.io.IOException | ||
| 41 | { | ||
| 42 | os.write( (int)'i' ); | ||
| 43 | writePackedInt( os, iValue ); | ||
| 44 | } | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Possible TODO: have this return a Number, and construct either a Long | ||
| 48 | * or BigInteger when appropriate. | ||
| 49 | */ | ||
| 50 | public static long readPackedInt( InputStream is ) throws java.io.IOException | ||
| 51 | { | ||
| 52 | int b; | ||
| 53 | long rOut = 0; | ||
| 54 | boolean bNeg; | ||
| 55 | |||
| 56 | b = is.read(); | ||
| 57 | bNeg = (b&0x40) == 0x40; | ||
| 58 | rOut |= b&0x3F; | ||
| 59 | int c = 0; | ||
| 60 | while( (b&0x80) == 0x80 ) | ||
| 61 | { | ||
| 62 | b = is.read(); | ||
| 63 | rOut |= (long)(b&0x7F) << (6+7*(c++)); | ||
| 64 | } | ||
| 65 | if( bNeg ) | ||
| 66 | return -rOut; | ||
| 67 | return rOut; | ||
| 68 | } | ||
| 69 | |||
| 70 | public static void writePackedInt( OutputStream os, long iIn ) throws java.io.IOException | ||
| 71 | { | ||
| 72 | int b; | ||
| 73 | |||
| 74 | if( iIn < 0 ) | ||
| 75 | { | ||
| 76 | iIn = -iIn; | ||
| 77 | b = (int)(iIn&0x3F); | ||
| 78 | if( iIn > b ) | ||
| 79 | b |= 0x80 | 0x40; | ||
| 80 | else | ||
| 81 | b |= 0x40; | ||
| 82 | } | ||
| 83 | else | ||
| 84 | { | ||
| 85 | b = (int)(iIn&0x3F); | ||
| 86 | if( iIn > b ) | ||
| 87 | b |= 0x80; | ||
| 88 | } | ||
| 89 | os.write( b ); | ||
| 90 | iIn = iIn >> 6; | ||
| 91 | |||
| 92 | while( iIn > 0 ) | ||
| 93 | { | ||
| 94 | b = (int)(iIn&0x7F); | ||
| 95 | if( iIn > b ) | ||
| 96 | b |= 0x80; | ||
| 97 | os.write( b ); | ||
| 98 | iIn = iIn >> 7; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | }; | ||
| 102 | |||
