KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > util > ModestInputStream


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.util;
26
27 final class ModestInputStream {
28     private byte bytes[];
29     private int index;
30     private boolean little_endian;
31     public ModestInputStream(byte bytes[]) {
32         this.bytes = bytes;
33         little_endian = bytes[0] != 0;
34         index = 1;
35     }
36
37     public boolean GetEndian() {
38         return little_endian;
39     }
40
41     public byte ReadOctet() {
42         return bytes[index++];
43     }
44
45     public short ReadUShort() {
46         index += index % 2;
47
48         if (little_endian) {
49             return (short)(((bytes[index++]) & 0x00ff) | ((bytes[index++] << 8) & 0xff00));
50         } else {
51             return (short)(((bytes[index++] << 8) & 0xff00) | ((bytes[index++]) & 0x00ff));
52         }
53     }
54
55     public int ReadULong() {
56         int al = index % 4;
57
58         if (al != 0) {
59             index += 4 - al;
60         }
61
62         if (little_endian) {
63             return ((bytes[index++]) & 0x000000ff) | ((bytes[index++] << 8) & 0x0000ff00) | ((bytes[index++] << 16) & 0x00ff0000) | ((bytes[index++] << 24) & 0xff000000);
64         } else {
65             return ((bytes[index++] << 24) & 0xff000000) | ((bytes[index++] << 16) & 0x00ff0000) | ((bytes[index++] << 8) & 0x0000ff00) | ((bytes[index++]) & 0x000000ff);
66         }
67     }
68
69     public String JavaDoc ReadString() {
70         int len = ReadULong() - 1;
71         String JavaDoc str = new String JavaDoc(bytes, index, len);
72         index += len;
73
74         if (ReadOctet() != 0) {
75             throw new org.omg.CORBA.INV_OBJREF JavaDoc("String should end with byte 0");
76         }
77
78         return str;
79     }
80
81     public byte[] ReadOctetArray() {
82         int len = ReadULong();
83         byte b[] = new byte[len];
84         System.arraycopy(bytes, index, b, 0, len);
85         index += len;
86         return b;
87     }
88 }
89
Popular Tags