Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/neuron/jaffer/ByteReader.java


1   /* 
2    * Copyright (c) 2003 Stewart Allen <stewart@neuron.com>. All rights reserved.
3    * This program is free software. See the 'License' file for details.
4    */
5   
6   package com.neuron.jaffer;
7   
8   import java.io.*;
9   import java.net.*;
10  import java.util.*;
11  
12  // TODO: implement readAFPString correctly (unicode)
13  public final class ByteReader extends Utility
14  {
15    private byte data[];
16    private int pos;
17    private int max;
18  
19    ByteReader(byte b[])
20    {
21      this(b, b.length);
22    }
23  
24    ByteReader(byte b[], int max)
25    {
26      this.max = max;
27      this.data = b;
28      this.pos = 0;
29    }
30  
31    public long writeToFile(RandomAccessFile file, long length)
32      throws IOException
33    {
34      int max = (int)Math.min(length, (long)(data.length - pos));
35      file.write(data, pos, max);
36      return (long)max;
37    }
38  
39    public int getPosition()
40    {
41      return pos;
42    }
43  
44    public boolean hasMoreData()
45    {
46      return pos < max;
47    }
48  
49    public int getAvailable()
50    {
51      return max - pos;
52    }
53  
54    public void seek(int pos)
55    {
56      this.pos = pos;
57    }
58  
59    public int skip(int len)
60    {
61      pos += len;
62      return len;
63    }
64  
65    public int skipBytes(int len)
66    {
67      pos += len;
68      return len;
69    }
70  
71    public byte[] readBytes(int len)
72    {
73      byte d[] = new byte[len];
74      readBytes(d);
75      return d;
76    }
77  
78    public void readBytes(byte b[])
79    {
80      System.arraycopy(data,pos,b,0,b.length);
81      pos += b.length;
82    }
83  
84    public void readBytes(byte b[], int off, int len)
85    {
86      System.arraycopy(data,pos,b,off,len);
87      pos += len;
88    }
89  
90    public String readString(int len)
91    {
92      byte b[] = new byte[len];
93      readBytes(b);
94      return new String(b);
95    }
96  
97    public String readCString(int max)
98    {
99      int scan = 0;
100     while (data[pos+scan] != 0)
101     {
102       scan++;
103       if (scan == max)
104       {
105         return null;
106       }
107     }
108     String s = new String(data, pos, scan);
109     pos += scan;
110     return s;
111   }
112 
113   public String readPString()
114   {
115     String s = readPString(data,pos);
116     pos += s.length() + 1;
117     return s;
118   }
119 
120   public String[] readPStringArray()
121   {
122     String s[] = readPStringArray(data,pos);
123     for (int i=0; i<s.length; i++)
124     {
125       pos += s[i].length() + 1;
126     }
127     return s;
128   }
129 
130   // read type byte. return PString for 1,2 and AFPString for 3
131   public String readTypedString()
132   {
133     if (readUnsignedByte() == 3)
134     {
135       return readAFPString();
136     }
137     else
138     {
139       return readPString();
140     }
141   }
142   
143   // unicode?
144   public String readAFPString()
145   {
146     String s = readAFPString(data,pos);
147     pos += s.length() + 2;
148     return s;
149   }
150   
151   public byte readByte()
152   {
153     return data[pos++];
154   }
155 
156   public int readUnsignedByte()
157   {
158     return data[pos++] & 0xff;
159   }
160 
161   public char readChar()
162   {
163     return (char)(((data[pos++]&0xff)<<8)|(data[pos++]&0xff)) ;
164   }
165 
166   public int readInt()
167   {
168     return (int)(
169       ((data[pos++]&0xff)<<24)|((data[pos++]&0xff)<<16)|
170       ((data[pos++]&0xff)<<8)|(data[pos++]&0xff)
171     );
172   }
173 
174   public long readLong()
175   {
176     return (long)(
177       ((long)(data[pos++]&0xff)<<56)|((long)(data[pos++]&0xff)<<48)|
178       ((long)(data[pos++]&0xff)<<40)|((long)(data[pos++]&0xff)<<32)|
179       ((long)(data[pos++]&0xff)<<24)|((long)(data[pos++]&0xff)<<16)|
180       ((long)(data[pos++]&0xff)<<8) | (long)(data[pos++]&0xff)
181     );
182   }
183 
184   public short readShort()
185   {
186     return (short)(((data[pos++]&0xff)<<8)|(data[pos++]&0xff));
187   }
188 
189   public int readUnsignedShort()
190   {
191     return (readShort() & 0xffff);
192   }
193 }
194