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.IOException;
032: import java.io.OutputStream;
033:
034: public class MemoryStream extends StreamImpl {
035: private TempBuffer _head;
036: private TempBuffer _tail;
037:
038: @Override
039: public Path getPath() {
040: return new NullPath("temp:");
041: }
042:
043: /**
044: * A memory stream is writable.
045: */
046: @Override
047: public boolean canWrite() {
048: return true;
049: }
050:
051: /**
052: * Writes a buffer to the underlying stream.
053: *
054: * @param buffer the byte array to write.
055: * @param offset the offset into the byte array.
056: * @param length the number of bytes to write.
057: * @param isEnd true when the write is flushing a close.
058: */
059: @Override
060: public void write(byte[] buf, int offset, int length, boolean isEnd)
061: throws IOException {
062: while (offset < length) {
063: if (_tail == null || _tail._length >= _tail._buf.length)
064: addBuffer(TempBuffer.allocate());
065:
066: int sublen = _tail._buf.length - _tail._length;
067: if (length - offset < sublen)
068: sublen = length - offset;
069:
070: System.arraycopy(buf, offset, _tail._buf, _tail._length,
071: sublen);
072:
073: offset += sublen;
074: _tail._length += sublen;
075: }
076: }
077:
078: private void addBuffer(TempBuffer buf) {
079: buf._next = null;
080: buf._length = 0;
081: if (_tail != null) {
082: _tail._next = buf;
083: _tail = buf;
084: } else {
085: _tail = buf;
086: _head = buf;
087: }
088: _head._bufferCount++;
089: }
090:
091: public void writeToStream(OutputStream os) throws IOException {
092: for (TempBuffer node = _head; node != null; node = node._next) {
093: os.write(node._buf, 0, node._length);
094: }
095: }
096:
097: public int getLength() {
098: if (_tail == null)
099: return 0;
100: else
101: return (_head._bufferCount - 1) * _head._length
102: + _tail._length;
103: }
104:
105: public ReadStream openReadAndSaveBuffer() throws IOException {
106: close();
107:
108: TempReadStream read = new TempReadStream(_head);
109: read.setFreeWhenDone(false);
110:
111: return new ReadStream(read);
112: }
113:
114: public void destroy() {
115: TempBuffer ptr;
116: TempBuffer next;
117:
118: ptr = _head;
119: _head = null;
120: _tail = null;
121:
122: for (; ptr != null; ptr = next) {
123: next = ptr._next;
124: TempBuffer.free(ptr);
125: ptr = null;
126: }
127: }
128: }
|