KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > MemoryStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33
34 public class MemoryStream extends StreamImpl {
35   private TempBuffer _head;
36   private TempBuffer _tail;
37
38   public Path getPath() { return new NullPath("temp:"); }
39
40   /**
41    * A memory stream is writable.
42    */

43   public boolean canWrite()
44   {
45     return true;
46   }
47   
48   /**
49    * Writes a buffer to the underlying stream.
50    *
51    * @param buffer the byte array to write.
52    * @param offset the offset into the byte array.
53    * @param length the number of bytes to write.
54    * @param isEnd true when the write is flushing a close.
55    */

56   public void write(byte []buf, int offset, int length, boolean isEnd)
57     throws IOException JavaDoc
58   {
59     while (offset < length) {
60       if (_tail == null || _tail._length >= _tail._buf.length)
61     addBuffer(TempBuffer.allocate());
62
63       int sublen = _tail._buf.length - _tail._length;
64       if (length - offset < sublen)
65     sublen = length - offset;
66
67       System.arraycopy(buf, offset, _tail._buf, _tail._length, sublen);
68
69       offset += sublen;
70       _tail._length += sublen;
71     }
72   }
73
74   private void addBuffer(TempBuffer buf)
75   {
76     buf._next = null;
77     buf._length = 0;
78     if (_tail != null) {
79       _tail._next = buf;
80       _tail = buf;
81     } else {
82       _tail = buf;
83       _head = buf;
84     }
85     _head._bufferCount++;
86   }
87
88   public void writeToStream(OutputStream JavaDoc os) throws IOException JavaDoc
89   {
90     for (TempBuffer node = _head; node != null; node = node._next) {
91       os.write(node._buf, 0, node._length);
92     }
93   }
94
95   public int getLength()
96   {
97     if (_tail == null)
98       return 0;
99     else
100       return (_head._bufferCount - 1) * _head._length + _tail._length;
101   }
102
103   public ReadStream openRead()
104     throws IOException JavaDoc
105   {
106     close();
107
108     TempReadStream read = new TempReadStream(_head, getPath());
109     read.setFreeWhenDone(false);
110
111     return new ReadStream(read);
112   }
113
114   public void destroy()
115   {
116     TempBuffer next;
117
118     for (; _head != null; _head = next) {
119       next = _head._next;
120       _head.free(_head);
121     }
122
123     _head = null;
124     _tail = null;
125   }
126 }
127
Popular Tags