67
68 public static final void writeBigEndian(int value, byte[] array) {
69 writeBigEndian(value, array, 0);
70 }
71
72 public static final void writeBigEndian(int value, byte[] array,
73 int pos) {
74 array[pos++] = (byte)((value>>>24));
75 array[pos++] = (byte)((value>>>16));
76 array[pos++] = (byte)((value>>>8));
77 array[pos++] = (byte)(value);
78 }
79
80 /**
81 * Reads an integer value from a byte array in little endian form. This
82 * method allows the reading of two byte values as well as four bytes
83 * values both of which are needed in the Kerberos v5 GSS-API mechanism.
84 *
85 * @param data the array containing the bytes of the integer value
86 * @param pos the offset in the array
87 * @size the number of bytes to read from the array.
88 * @return the integer value
89 */
90 public static final int readLittleEndian(byte[] data, int pos, int size) {
91 int retVal = 0;
92 int shifter = 0;
93 while (size > 0) {
94 retVal += (data[pos] & 0xff) << shifter;
95 shifter += 8;
96 pos++;
97 size--;
98 }
99 return retVal;
100 }
101
102 public static final int readBigEndian(byte[] data, int pos, int size) {
103 int retVal = 0;
104 int shifter = (size-1)*8;
105 while (size > 0) {
106 retVal += (data[pos] & 0xff) << shifter;
107 shifter -= 8;
124 os.write(val);
125 }
126
127 /**
128 * Writes a two byte integer value to a byte array.
129 *
130 * @param val the integer value. It will lose the high-order two bytes.
131 * @param dest the byte array to write to
132 * @param pos the offset to start writing to
133 */
134 public static final int writeInt(int val, byte[] dest, int pos) {
135 dest[pos++] = (byte)(val>>>8);
136 dest[pos++] = (byte)val;
137 return pos;
138 }
139
140 /**
141 * Reads a two byte integer value from an InputStream.
142 *
143 * @param is the InputStream to read from
144 * @returns the integer value
145 * @throws IOException if some errors occurs while reading the integer
146 * bytes.
147 */
148 public static final int readInt(InputStream is) throws IOException {
149 return (((0xFF & is.read()) << 8)
150 | (0xFF & is.read()));
151 }
152
153 /**
154 * Reads a two byte integer value from a byte array.
155 *
156 * @param src the byte arra to read from
157 * @param pos the offset to start reading from
158 * @returns the integer value
159 */
160 public static final int readInt(byte[] src, int pos) {
161 return ((0xFF & src[pos])<<8 | (0xFF & src[pos+1]));
162 }
163
164 /**
165 * Blocks till the required number of bytes have been read from the
166 * input stream.
167 *
168 * @param is the InputStream to read from
169 * @param buffer the buffer to store the bytes into
170 * @param throws EOFException if EOF is reached before all bytes are
171 * read.
172 * @throws IOException is an error occurs while reading
173 */
174 public static final void readFully(InputStream is, byte[] buffer)
175 throws IOException {
176 readFully(is, buffer, 0, buffer.length);
177 }
178
179 /**
180 * Blocks till the required number of bytes have been read from the
181 * input stream.
182 *
183 * @param is the InputStream to read from
184 * @param buffer the buffer to store the bytes into
185 * @param offset the offset to start storing at
186 * @param len the number of bytes to read
187 * @param throws EOFException if EOF is reached before all bytes are
188 * read.
189 * @throws IOException is an error occurs while reading
190 */
191 public static final void readFully(InputStream is,
192 byte[] buffer, int offset, int len)
193 throws IOException {
194 int temp;
195 while (len > 0) {
196 temp = is.read(buffer, offset, len);
197 if (temp == -1)
198 throw new EOFException("Cannot read all "
199 + len
200 + " bytes needed to form this token!");
201 offset += temp;
202 len -= temp;
203 }
204 }
205
206 public static final void debug(String str) {
207 System.err.print(str);
|
67
68 public static final void writeBigEndian(int value, byte[] array) {
69 writeBigEndian(value, array, 0);
70 }
71
72 public static final void writeBigEndian(int value, byte[] array,
73 int pos) {
74 array[pos++] = (byte)((value>>>24));
75 array[pos++] = (byte)((value>>>16));
76 array[pos++] = (byte)((value>>>8));
77 array[pos++] = (byte)(value);
78 }
79
80 /**
81 * Reads an integer value from a byte array in little endian form. This
82 * method allows the reading of two byte values as well as four bytes
83 * values both of which are needed in the Kerberos v5 GSS-API mechanism.
84 *
85 * @param data the array containing the bytes of the integer value
86 * @param pos the offset in the array
87 * @param size the number of bytes to read from the array.
88 * @return the integer value
89 */
90 public static final int readLittleEndian(byte[] data, int pos, int size) {
91 int retVal = 0;
92 int shifter = 0;
93 while (size > 0) {
94 retVal += (data[pos] & 0xff) << shifter;
95 shifter += 8;
96 pos++;
97 size--;
98 }
99 return retVal;
100 }
101
102 public static final int readBigEndian(byte[] data, int pos, int size) {
103 int retVal = 0;
104 int shifter = (size-1)*8;
105 while (size > 0) {
106 retVal += (data[pos] & 0xff) << shifter;
107 shifter -= 8;
124 os.write(val);
125 }
126
127 /**
128 * Writes a two byte integer value to a byte array.
129 *
130 * @param val the integer value. It will lose the high-order two bytes.
131 * @param dest the byte array to write to
132 * @param pos the offset to start writing to
133 */
134 public static final int writeInt(int val, byte[] dest, int pos) {
135 dest[pos++] = (byte)(val>>>8);
136 dest[pos++] = (byte)val;
137 return pos;
138 }
139
140 /**
141 * Reads a two byte integer value from an InputStream.
142 *
143 * @param is the InputStream to read from
144 * @return the integer value
145 * @throws IOException if some errors occurs while reading the integer
146 * bytes.
147 */
148 public static final int readInt(InputStream is) throws IOException {
149 return (((0xFF & is.read()) << 8)
150 | (0xFF & is.read()));
151 }
152
153 /**
154 * Reads a two byte integer value from a byte array.
155 *
156 * @param src the byte arra to read from
157 * @param pos the offset to start reading from
158 * @return the integer value
159 */
160 public static final int readInt(byte[] src, int pos) {
161 return ((0xFF & src[pos])<<8 | (0xFF & src[pos+1]));
162 }
163
164 /**
165 * Blocks till the required number of bytes have been read from the
166 * input stream.
167 *
168 * @param is the InputStream to read from
169 * @param buffer the buffer to store the bytes into
170 * @throws EOFException if EOF is reached before all bytes are
171 * read.
172 * @throws IOException is an error occurs while reading
173 */
174 public static final void readFully(InputStream is, byte[] buffer)
175 throws IOException {
176 readFully(is, buffer, 0, buffer.length);
177 }
178
179 /**
180 * Blocks till the required number of bytes have been read from the
181 * input stream.
182 *
183 * @param is the InputStream to read from
184 * @param buffer the buffer to store the bytes into
185 * @param offset the offset to start storing at
186 * @param len the number of bytes to read
187 * @throws EOFException if EOF is reached before all bytes are
188 * read.
189 * @throws IOException is an error occurs while reading
190 */
191 public static final void readFully(InputStream is,
192 byte[] buffer, int offset, int len)
193 throws IOException {
194 int temp;
195 while (len > 0) {
196 temp = is.read(buffer, offset, len);
197 if (temp == -1)
198 throw new EOFException("Cannot read all "
199 + len
200 + " bytes needed to form this token!");
201 offset += temp;
202 len -= temp;
203 }
204 }
205
206 public static final void debug(String str) {
207 System.err.print(str);
|