001: /*
002: * %W% %E%
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.jump.message;
028:
029: import java.io.IOException;
030:
031: /**
032: * <code>JUMPMessageReader</code> is used to read data from a message.
033: */
034: public class JUMPMessageReader {
035: private final byte[] messageDataBytes;
036: private final int messageMarkOffset;
037: int messageDataOffset; // Package private access for JUMPMessage
038:
039: /**
040: * Creates a new instance of JUMPMessageReader for message m
041: */
042: public JUMPMessageReader(JUMPMessage m) {
043: this (m, m.messageUserDataOffset);
044: }
045:
046: /**
047: * Package private constructor to set initial offset to
048: * start reading from.
049: */
050: JUMPMessageReader(JUMPMessage m, int offset) {
051: this .messageDataBytes = m.messageDataBytes;
052: // Context starts from user data offset, past the header
053: this .messageMarkOffset = offset;
054: this .messageDataOffset = offset;
055: }
056:
057: public int getInt() {
058: // Get int at messageDataOffset
059: int b1 = messageDataBytes[messageDataOffset] & 0xff;
060: int b2 = messageDataBytes[messageDataOffset + 1] & 0xff;
061: int b3 = messageDataBytes[messageDataOffset + 2] & 0xff;
062: int b4 = messageDataBytes[messageDataOffset + 3] & 0xff;
063: int i = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
064: if (false) {
065: System.err.println("Decoded int " + i + " from [" + b1
066: + ", " + b2 + ", " + b3 + ", " + b4 + "]");
067: }
068: messageDataOffset += 4;
069: return i;
070: }
071:
072: public long getLong() {
073: long l = (((long) messageDataBytes[messageDataOffset + 0] << 56)
074: + ((long) (messageDataBytes[messageDataOffset + 1] & 255) << 48)
075: + ((long) (messageDataBytes[messageDataOffset + 2] & 255) << 40)
076: + ((long) (messageDataBytes[messageDataOffset + 3] & 255) << 32)
077: + ((long) (messageDataBytes[messageDataOffset + 4] & 255) << 24)
078: + ((messageDataBytes[messageDataOffset + 5] & 255) << 16)
079: + ((messageDataBytes[messageDataOffset + 6] & 255) << 8) + ((messageDataBytes[messageDataOffset + 7] & 255) << 0));
080: messageDataOffset += 8;
081: return l;
082: }
083:
084: public byte getByte() {
085: byte b = messageDataBytes[messageDataOffset];
086: messageDataOffset += 1;
087: return b;
088: }
089:
090: private char[] fromBytes(byte[] ascii) {
091: int n = ascii.length;
092: char[] value = new char[n - 1]; // Leave out the null termination
093:
094: for (int i = 0; i < n - 1; i++) {
095: value[i] = (char) (ascii[i] & 0xff);
096: }
097: return value;
098: }
099:
100: public String getUTF() {
101: // FIXME: GEt string in Java-modified utf-8
102: // Re-use implementation in DataInputStream.readUTF().
103: return new String(fromBytes(getByteArray()));
104: }
105:
106: public byte[] getByteArray() {
107: int len = getInt();
108: if (len == -1) {
109: return null;
110: }
111: byte[] b = new byte[len];
112: System
113: .arraycopy(messageDataBytes, messageDataOffset, b, 0,
114: len);
115: messageDataOffset += len;
116: return b;
117: }
118:
119: public String[] getUTFArray() {
120: int len = getInt();
121: if (len == -1) {
122: return null;
123: }
124: String[] s = new String[len];
125: for (int i = 0; i < len; i++) {
126: s[i] = getUTF();
127: }
128: return s;
129: }
130:
131: public void reset() {
132: messageDataOffset = messageMarkOffset;
133: }
134:
135: }
|