001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.tcp.io;
038:
039: import java.io.EOFException;
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.io.OutputStream;
043: import java.nio.ByteBuffer;
044:
045: /**
046: * @author Alexey Stashok
047: */
048: public final class DataInOutUtils {
049: public static int readInt4(final InputStream is) throws IOException {
050: int value = 0;
051:
052: for (int shVal = 0, neeble = 8; (neeble & 8) != 0; shVal += 6) {
053: final int octet = is.read();
054: if (octet == -1) {
055: throw new EOFException();
056: }
057: neeble = octet >> 4;
058:
059: value |= ((neeble & 7) << shVal);
060: if ((neeble & 8) != 0) {
061: neeble = octet & 0xF;
062: value |= ((neeble & 7) << (shVal + 3));
063: }
064: }
065:
066: return value;
067: }
068:
069: public static void readInts4(final InputStream is,
070: final int[] array, final int count) throws IOException {
071: readInts4(is, array, count, 0);
072: }
073:
074: public static int readInts4(final InputStream is,
075: final int[] array, final int count, final int lowValue)
076: throws IOException {
077: int value = 0;
078: int octet = 0;
079: int readInts = 0;
080: int shVal = 0;
081: int neeble = 0;
082: int neebleNum = 0;
083:
084: if (lowValue > 0) {
085: octet = lowValue & 0xF;
086: neebleNum = 1;
087: }
088:
089: for (; readInts < count; neebleNum++) {
090: if (neebleNum % 2 == 0) {
091: octet = is.read();
092: if (octet == -1) {
093: throw new EOFException();
094: }
095: neeble = octet >> 4;
096: } else {
097: neeble = octet & 0xF;
098: }
099:
100: value |= ((neeble & 7) << shVal);
101: if ((neeble & 8) == 0) {
102: array[readInts++] = value;
103: shVal = 0;
104: value = 0;
105: } else {
106: shVal += 3;
107: }
108: }
109:
110: if (neebleNum % 2 != 0) {
111: return 0x80 | (octet & 0xF);
112: }
113:
114: return 0;
115: }
116:
117: public static void readInts4(final ByteBuffer buffer,
118: final int[] array, final int count) throws IOException {
119: readInts4(buffer, array, count, 0);
120: }
121:
122: public static int readInts4(final ByteBuffer buffer,
123: final int[] array, final int count, final int lowValue)
124: throws IOException {
125: int value = 0;
126: int octet = 0;
127: int readInts = 0;
128: int shVal = 0;
129: int neeble = 0;
130: int neebleNum = 0;
131:
132: if (lowValue > 0) {
133: octet = lowValue & 0xF;
134: neebleNum = 1;
135: }
136:
137: for (; readInts < count; neebleNum++) {
138: if (neebleNum % 2 == 0) {
139: if (!buffer.hasRemaining()) {
140: throw new EOFException();
141: }
142: octet = buffer.get();
143:
144: neeble = octet >> 4;
145: } else {
146: neeble = octet & 0xF;
147: }
148:
149: value |= ((neeble & 7) << shVal);
150: if ((neeble & 8) == 0) {
151: array[readInts++] = value;
152: shVal = 0;
153: value = 0;
154: } else {
155: shVal += 3;
156: }
157: }
158:
159: if (neebleNum % 2 != 0) {
160: return 0x80 | (octet & 0xF);
161: }
162:
163: return 0;
164: }
165:
166: public static void writeInt4(final OutputStream os, int value)
167: throws IOException {
168: int nibbleL;
169: int nibbleH;
170: do {
171: nibbleH = value & 7;
172: value >>>= 3;
173:
174: if (value != 0) {
175: nibbleH |= 8;
176: nibbleL = value & 7;
177: value >>>= 3;
178: if (value != 0) {
179: nibbleL |= 8;
180: }
181: } else {
182: nibbleL = 0;
183: }
184:
185: os.write(nibbleL | (nibbleH << 4));
186: } while (value != 0);
187: }
188:
189: public static int readInt8(final InputStream is) throws IOException {
190: int value = 0;
191: for (int shVal = 0, octet = 0x80; (octet & 0x80) != 0; shVal += 7) {
192: octet = is.read();
193: if (octet == -1) {
194: throw new EOFException();
195: }
196:
197: value |= ((octet & 0x7F) << shVal);
198: }
199:
200: return value;
201: }
202:
203: public static void writeInt8(final OutputStream os, int value)
204: throws IOException {
205: int octet;
206: do {
207: octet = value & 0x7F;
208: value >>>= 7;
209:
210: if (value != 0) {
211: octet |= 0x80;
212: }
213:
214: os.write(octet);
215: } while (value != 0);
216: }
217:
218: public static void writeInt8(final ByteBuffer bb, int value)
219: throws IOException {
220: int octet;
221: do {
222: octet = value & 0x7F;
223: value >>>= 7;
224:
225: if (value != 0) {
226: octet |= 0x80;
227: }
228:
229: bb.put((byte) octet);
230: } while (value != 0);
231: }
232:
233: public static int readInt4(final ByteBuffer buffer)
234: throws IOException {
235: int value = 0;
236:
237: for (int shVal = 0, neeble = 8; (neeble & 8) != 0; shVal += 6) {
238: if (!buffer.hasRemaining()) {
239: throw new EOFException();
240: }
241:
242: final int octet = buffer.get();
243: neeble = octet >> 4;
244:
245: value |= ((neeble & 7) << shVal);
246: if ((neeble & 8) != 0) {
247: neeble = octet & 0xF;
248: value |= ((neeble & 7) << (shVal + 3));
249: }
250: }
251:
252: return value;
253: }
254:
255: public static void writeInts4(final ByteBuffer bb,
256: final int... values) throws IOException {
257: writeInts4(bb, values, 0, values.length);
258: }
259:
260: public static void writeInts4(final ByteBuffer bb,
261: final int[] array, final int offset, final int count)
262: throws IOException {
263: int shiftValue = 0;
264: for (int i = 0; i < count - 1; i++) {
265: final int value = array[offset + i];
266: shiftValue = writeInt4(bb, value, shiftValue, false);
267: }
268:
269: if (count > 0) {
270: writeInt4(bb, array[offset + count - 1], shiftValue, true);
271: }
272: }
273:
274: public static void writeInts4(final OutputStream out,
275: final int... values) throws IOException {
276: writeInts4(out, values, 0, values.length);
277: }
278:
279: public static void writeInts4(final OutputStream out,
280: final int[] array, final int offset, final int count)
281: throws IOException {
282: int shiftValue = 0;
283: for (int i = 0; i < count - 1; i++) {
284: final int value = array[offset + i];
285: shiftValue = writeInt4(out, value, shiftValue, false);
286: }
287:
288: if (count > 0) {
289: writeInt4(out, array[offset + count - 1], shiftValue, true);
290: }
291: }
292:
293: public static int writeInt4(final OutputStream out, int value,
294: int highValue, final boolean flush) throws IOException {
295: int nibbleL;
296: int nibbleH;
297:
298: if (highValue > 0) {
299: highValue &= 0x70; // clear highest bit
300: nibbleL = value & 7;
301: value >>>= 3;
302: if (value != 0) {
303: nibbleL |= 8;
304: }
305:
306: out.write(highValue | nibbleL);
307:
308: if (value == 0) {
309: return 0;
310: }
311: }
312:
313: do {
314: // shift nibbleH to high byte's bits
315: nibbleH = (value & 7) << 4;
316: value >>>= 3;
317:
318: if (value != 0) {
319: nibbleH |= 0x80;
320: nibbleL = value & 7;
321: value >>>= 3;
322: if (value != 0) {
323: nibbleL |= 8;
324: }
325: } else {
326: if (!flush) {
327: return nibbleH | 0x80;
328: }
329:
330: nibbleL = 0;
331: }
332:
333: out.write(nibbleH | nibbleL);
334: } while (value != 0);
335:
336: return 0;
337: }
338:
339: public static int writeInt4(final ByteBuffer bb, int value,
340: int highValue, final boolean flush) throws IOException {
341: int nibbleL;
342: int nibbleH;
343:
344: if (highValue > 0) {
345: highValue &= 0x70; // clear highest bit
346: nibbleL = value & 7;
347: value >>>= 3;
348: if (value != 0) {
349: nibbleL |= 8;
350: }
351:
352: bb.put((byte) (highValue | nibbleL));
353:
354: if (value == 0) {
355: return 0;
356: }
357: }
358:
359: do {
360: // shift nibbleH to high byte's bits
361: nibbleH = (value & 7) << 4;
362: value >>>= 3;
363:
364: if (value != 0) {
365: nibbleH |= 0x80;
366: nibbleL = value & 7;
367: value >>>= 3;
368: if (value != 0) {
369: nibbleL |= 8;
370: }
371: } else {
372: if (!flush) {
373: return nibbleH | 0x80;
374: }
375:
376: nibbleL = 0;
377: }
378:
379: bb.put((byte) (nibbleH | nibbleL));
380: } while (value != 0);
381:
382: return 0;
383: }
384:
385: public static int readInt8(final ByteBuffer buffer)
386: throws IOException {
387: int value = 0;
388: for (int shVal = 0, octet = 0x80; (octet & 0x80) != 0; shVal += 7) {
389: if (!buffer.hasRemaining()) {
390: throw new EOFException();
391: }
392:
393: octet = buffer.get();
394:
395: value |= ((octet & 0x7F) << shVal);
396: }
397:
398: return value;
399: }
400:
401: public static void readFully(final InputStream inputStream,
402: final byte[] buffer) throws IOException {
403: readFully(inputStream, buffer, 0, buffer.length);
404: }
405:
406: public static void readFully(final InputStream inputStream,
407: final byte[] buffer, final int offset, final int length)
408: throws IOException {
409: int bytesRead = 0;
410: while (bytesRead < length) {
411: final int count = inputStream.read(buffer, offset
412: + bytesRead, length - bytesRead);
413: if (count < 0) {
414: throw new EOFException();
415: }
416: bytesRead += count;
417: }
418: }
419: }
|