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 com.caucho.vfs.TempBuffer;
032:
033: public class EncapsulationMessageWriter extends MessageWriter {
034: private TempBuffer _head;
035: private TempBuffer _tail;
036:
037: private byte[] _buffer;
038:
039: private int _offset;
040:
041: private int _length;
042:
043: public EncapsulationMessageWriter() {
044: _head = _tail = TempBuffer.allocate();
045:
046: _buffer = _head.getBuffer();
047: }
048:
049: /**
050: * Returns the offset.
051: */
052: public int getOffset() {
053: return _offset;
054: }
055:
056: /**
057: * Writes a byte.
058: */
059: public void write(int v) {
060: if (_buffer.length <= _length)
061: flushBuffer();
062:
063: _offset++;
064: _buffer[_length++] = (byte) v;
065: }
066:
067: /**
068: * Writes data
069: */
070: public void write(byte[] buffer, int offset, int length) {
071: while (length > 0) {
072: int sublen = _buffer.length - _length;
073:
074: if (length < sublen)
075: sublen = length;
076:
077: System.arraycopy(buffer, offset, _buffer, _length, sublen);
078:
079: _offset += sublen;
080: _length += sublen;
081: length -= sublen;
082:
083: if (length > 0)
084: flushBuffer();
085: }
086: }
087:
088: /**
089: * Writes a short
090: */
091: public final void writeShort(int v) {
092: if (_buffer.length <= _length + 1)
093: flushBuffer();
094:
095: _offset += 2;
096:
097: _buffer[_length++] = (byte) (v >> 8);
098: _buffer[_length++] = (byte) (v);
099: }
100:
101: /**
102: * Writes an integer.
103: */
104: public void writeInt(int v) {
105: if (_buffer.length <= _length + 3)
106: flushBuffer();
107:
108: _offset += 4;
109:
110: _buffer[_length++] = (byte) (v >> 24);
111: _buffer[_length++] = (byte) (v >> 16);
112: _buffer[_length++] = (byte) (v >> 8);
113: _buffer[_length++] = (byte) (v);
114: }
115:
116: /**
117: * Aligns to a specified value.
118: */
119: public void align(int v) {
120: int delta = v - _length % v;
121:
122: if (delta == v)
123: return;
124:
125: _offset += delta;
126:
127: for (; delta > 0; delta--)
128: _buffer[_length++] = 0;
129: }
130:
131: /**
132: * Flushes the buffer.
133: */
134: private void flushBuffer() {
135: _tail.setLength(_length);
136:
137: TempBuffer tail = TempBuffer.allocate();
138: _tail.setNext(tail);
139: _tail = tail;
140:
141: _buffer = _tail.getBuffer();
142: _length = 0;
143: }
144:
145: /**
146: * Completes the response.
147: */
148: public void close() {
149: _tail.setLength(_length);
150: _tail = null;
151: }
152:
153: public void writeToWriter(IiopWriter out) {
154: TempBuffer ptr = _head;
155: int fullLen = 0;
156:
157: while (ptr != null) {
158: TempBuffer next = ptr.getNext();
159:
160: int len = ptr.getLength();
161: byte[] buffer = ptr.getBuffer();
162:
163: out.write(buffer, 0, len);
164:
165: fullLen += len;
166:
167: TempBuffer.free(ptr);
168: ptr = next;
169: }
170: }
171: }
|