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.*;
032:
033: import com.caucho.vfs.*;
034:
035: class InputStreamMessageReader extends MessageReader {
036: private InputStream _is;
037: private int _offset;
038: private int _chunkOffset;
039: private int _align;
040:
041: private TempBuffer _tempBuffer;
042: private byte[] _buffer;
043:
044: private int _length;
045: private boolean _isLast;
046:
047: InputStreamMessageReader(InputStream is, boolean isLast, int offset) {
048: try {
049: _is = is;
050:
051: _isLast = isLast;
052:
053: _length = (((_is.read() & 0xff) << 24)
054: + ((_is.read() & 0xff) << 16)
055: + ((_is.read() & 0xff) << 8) + ((_is.read() & 0xff)));
056:
057: if (_length <= TempBuffer.SIZE) {
058: _tempBuffer = TempBuffer.allocate();
059: _buffer = _tempBuffer.getBuffer();
060: } else
061: _buffer = new byte[_length + (1024 - _length % 1024)
062: % 1024];
063:
064: is.read(_buffer, 0, _length);
065:
066: // writeHexGroup(_buffer, 0, _length);
067:
068: offset = 12;
069:
070: _chunkOffset = offset;
071: _offset = offset;
072: _align = 0;
073: } catch (IOException e) {
074: throw new RuntimeException(e);
075: }
076: }
077:
078: /**
079: * Returns the offset.
080: */
081: public int getOffset() {
082: return _offset + _align;
083: }
084:
085: /**
086: * Sets the offset.
087: */
088: public int setOffset(int offset) {
089: int oldOffset = _offset + _align;
090:
091: _align = offset - _offset;
092:
093: return oldOffset;
094: }
095:
096: /**
097: * Reads a byte.
098: */
099: public int read() {
100: try {
101: if (_length <= _offset - _chunkOffset)
102: readFragmentHeader();
103:
104: return _buffer[_offset++ - _chunkOffset] & 0xff;
105: } catch (IOException e) {
106: throw new RuntimeException(e);
107: }
108: }
109:
110: /**
111: * Reads data
112: */
113: public void read(byte[] buffer, int offset, int length) {
114: try {
115: while (length > 0) {
116: int sublen = _length - (_offset - _chunkOffset);
117:
118: if (sublen <= 0) {
119: readFragmentHeader();
120: sublen = _length - (_offset - _chunkOffset);
121: }
122:
123: if (length < sublen)
124: sublen = length;
125:
126: System.arraycopy(_buffer, _offset - _chunkOffset,
127: buffer, offset, sublen);
128:
129: if (sublen < 0)
130: throw new IOException("unexpected end of file");
131:
132: offset += sublen;
133: length -= sublen;
134: _offset += sublen;
135: }
136: } catch (IOException e) {
137: throw new RuntimeException(e);
138: }
139: }
140:
141: private void readFragmentHeader() throws IOException {
142: if (_isLast)
143: throw new EOFException("read past end of file");
144:
145: if (_is.read() != 'G' || _is.read() != 'I' || _is.read() != 'O'
146: || _is.read() != 'P')
147: throw new IOException("Missing GIOP header.");
148:
149: int major = _is.read();
150: int minor = _is.read();
151:
152: if (major != 1)
153: throw new IOException("unknown major");
154:
155: int flags = _is.read();
156: int type = _is.read();
157:
158: if (type != IiopReader.MSG_FRAGMENT)
159: throw new IOException("expected fragment at " + type);
160:
161: _isLast = (flags & 2) != 2;
162:
163: _chunkOffset = _offset;
164: _length = (((_is.read() & 0xff) << 24)
165: + ((_is.read() & 0xff) << 16)
166: + ((_is.read() & 0xff) << 8) + ((_is.read() & 0xff)));
167:
168: if (minor >= 2) {
169: int reqId = (((_is.read() & 0xff) << 24)
170: + ((_is.read() & 0xff) << 16)
171: + ((_is.read() & 0xff) << 8) + ((_is.read() & 0xff)));
172:
173: _length -= 4;
174: }
175:
176: if (_length < 0 || _length > 65536)
177: throw new RuntimeException();
178:
179: _is.read(_buffer, 0, _length);
180: }
181:
182: private void writeHexGroup(byte[] buffer, int offset, int length) {
183: int end = offset + length;
184:
185: while (offset < end) {
186: int chunkLength = 16;
187:
188: for (int j = 0; j < chunkLength; j++) {
189: System.out.print(" ");
190: printHex(buffer[offset + j]);
191: }
192:
193: System.out.print(" ");
194: for (int j = 0; j < chunkLength; j++) {
195: printCh(buffer[offset + j]);
196: }
197:
198: offset += chunkLength;
199:
200: System.out.println();
201: }
202: }
203:
204: private void printHex(int d) {
205: int ch1 = (d >> 4) & 0xf;
206: int ch2 = d & 0xf;
207:
208: if (ch1 >= 10)
209: System.out.print((char) ('a' + ch1 - 10));
210: else
211: System.out.print((char) ('0' + ch1));
212:
213: if (ch2 >= 10)
214: System.out.print((char) ('a' + ch2 - 10));
215: else
216: System.out.print((char) ('0' + ch2));
217: }
218:
219: private void printCh(int d) {
220: if (d >= 0x20 && d <= 0x7f)
221: System.out.print("" + ((char) d));
222: else
223: System.out.print(".");
224: }
225:
226: private String toCh(int d) {
227: if (d >= 0x20 && d <= 0x7f)
228: return "" + (char) d;
229: else
230: return "" + d;
231: }
232:
233: private static String toHex(int v) {
234: StringBuilder cb = new StringBuilder();
235: for (int i = 28; i >= 0; i -= 4) {
236: int h = (v >> i) & 0xf;
237:
238: if (h >= 10)
239: cb.append((char) ('a' + h - 10));
240: else
241: cb.append(h);
242: }
243:
244: return cb.toString();
245: }
246: }
|