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.vfs;
030:
031: import java.io.*;
032:
033: public class TempOutputStream extends OutputStream {
034: private TempBuffer _head;
035: private TempBuffer _tail;
036:
037: public byte[] getTail() {
038: return _tail.getBuffer();
039: }
040:
041: public void write(int ch) throws IOException {
042: if (_tail == null)
043: addBuffer(TempBuffer.allocate());
044: else if (_tail._buf.length <= _tail._length)
045: addBuffer(TempBuffer.allocate());
046:
047: _tail.getBuffer()[_tail._length++] = (byte) ch;
048: }
049:
050: @Override
051: public void write(byte[] buf, int offset, int length)
052: throws IOException {
053: int index = 0;
054: while (index < length) {
055: if (_tail == null)
056: addBuffer(TempBuffer.allocate());
057: else if (_tail._buf.length <= _tail._length)
058: addBuffer(TempBuffer.allocate());
059:
060: int sublen = _tail._buf.length - _tail._length;
061: if (length - index < sublen)
062: sublen = length - index;
063:
064: System.arraycopy(buf, index + offset, _tail._buf,
065: _tail._length, sublen);
066:
067: index += sublen;
068: _tail._length += sublen;
069: }
070: }
071:
072: private void addBuffer(TempBuffer buf) {
073: buf._next = null;
074: if (_tail != null) {
075: _tail._next = buf;
076: _tail = buf;
077: } else {
078: _tail = buf;
079: _head = buf;
080: }
081:
082: _head._bufferCount++;
083: }
084:
085: public void flush() throws IOException {
086: }
087:
088: @Override
089: public void close() throws IOException {
090: }
091:
092: /**
093: * Opens a read stream to the buffer.
094: */
095: public ReadStream openRead() throws IOException {
096: close();
097:
098: TempReadStream read = new TempReadStream(_head);
099: read.setFreeWhenDone(true);
100: _head = null;
101: _tail = null;
102:
103: return new ReadStream(read);
104: }
105:
106: /**
107: * Returns the head buffer.
108: */
109: public TempBuffer getHead() {
110: return _head;
111: }
112:
113: public void writeToStream(OutputStream os) throws IOException {
114: for (TempBuffer ptr = _head; ptr != null; ptr = ptr._next) {
115: os.write(ptr.getBuffer(), 0, ptr.getLength());
116: }
117: }
118:
119: /**
120: * Returns the total length of the buffer's bytes
121: */
122: public int getLength() {
123: int length = 0;
124:
125: for (TempBuffer ptr = _head; ptr != null; ptr = ptr._next) {
126: length += ptr.getLength();
127: }
128:
129: return length;
130: }
131:
132: /**
133: * Clean up the temp stream.
134: */
135: public void destroy() {
136: TempBuffer ptr = _head;
137:
138: _head = null;
139: _tail = null;
140:
141: TempBuffer.freeAll(ptr);
142: }
143: }
|