001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.iiop;
030:
031: import java.io.IOException;
032:
033: abstract public class MessageReader {
034: /**
035: * Returns the offset.
036: */
037: abstract public int getOffset();
038:
039: /**
040: * Sets the offset.
041: */
042: abstract public int setOffset(int offset);
043:
044: /**
045: * Sets the alignment (for sequences).
046: */
047: public void addOffset(int delta) {
048: setOffset(getOffset() + delta);
049: }
050:
051: /**
052: * Reads a byte.
053: */
054: abstract public int read();
055:
056: /**
057: * Reads data
058: */
059: public void read(byte[] buffer, int offset, int length) {
060: for (int i = 0; i < length; i++)
061: buffer[i + offset] = (byte) read();
062: }
063:
064: /**
065: * Reads a short (with alignment)
066: */
067: public int read_short() {
068: int offset = getOffset();
069:
070: while (offset % 2 != 0) {
071: offset++;
072: read();
073: }
074:
075: return (((read() & 0xff) << 8) + (read() & 0xff));
076: }
077:
078: /**
079: * Reads an integer
080: */
081: public int read_long() {
082: int offset = getOffset();
083:
084: while (offset % 4 != 0) {
085: offset++;
086: read();
087: }
088:
089: int v = (((read() & 0xff) << 24) + ((read() & 0xff) << 16)
090: + ((read() & 0xff) << 8) + (read() & 0xff));
091:
092: return v;
093: }
094:
095: /**
096: * Reads a long.
097: */
098: public long read_longlong() {
099: int offset = getOffset();
100:
101: while (offset % 8 != 0) {
102: offset++;
103: read();
104: }
105:
106: return (((read() & 0xffL) << 56) + ((read() & 0xffL) << 48)
107: + ((read() & 0xffL) << 40) + ((read() & 0xffL) << 32)
108: + ((read() & 0xffL) << 24) + ((read() & 0xffL) << 16)
109: + ((read() & 0xffL) << 8) + (read() & 0xffL));
110: }
111:
112: public void skip(int len) {
113: for (; len > 0; len--) {
114: read();
115: }
116: }
117:
118: /**
119: * Aligns to a specified value.
120: */
121: public void align(int v) {
122: int offset = getOffset();
123:
124: while (offset % v != 0) {
125: offset++;
126: read();
127: }
128: }
129:
130: /**
131: * Completes the request
132: */
133: public void close() {
134: }
135: }
|