KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.caucho.util.Crc64;
32
33 import java.io.IOException JavaDoc;
34
35 public class Crc64Stream extends StreamImpl {
36   private StreamImpl _next;
37   private long _crc;
38
39   public Crc64Stream(StreamImpl next)
40   {
41     _next = next;
42   }
43
44   public Crc64Stream()
45   {
46   }
47
48   /**
49    * Initialize the filter with a new stream.
50    */

51   public void init(StreamImpl next)
52   {
53     _next = next;
54     _crc = 0;
55   }
56
57   /**
58    * Returns the CRC value.
59    */

60   public long getCRC()
61   {
62     return _crc;
63   }
64
65   /**
66    * Returns true if the stream can read.
67    */

68   public boolean canRead()
69   {
70     return _next.canRead();
71   }
72
73
74   /**
75    * Reads a buffer from the underlying stream.
76    *
77    * @param buffer the byte array to read.
78    * @param offset the offset into the byte array.
79    * @param length the number of bytes to read.
80    */

81   public int read(byte []buffer, int offset, int length)
82     throws IOException JavaDoc
83   {
84     long crc = _crc;
85
86     int len = _next.read(buffer, offset, length);
87     
88     for (int i = 0; i < len; i++)
89       crc = Crc64.next(crc, buffer[offset + i]);
90
91     _crc = crc;
92
93     return len;
94   }
95
96   /**
97    * Returns true if the stream can write.
98    */

99   public boolean canWrite()
100   {
101     return _next.canWrite();
102   }
103
104
105   /**
106    * Writes a buffer to the underlying stream.
107    *
108    * @param buffer the byte array to write.
109    * @param offset the offset into the byte array.
110    * @param length the number of bytes to write.
111    * @param isEnd true when the write is flushing a close.
112    */

113   public void write(byte []buffer, int offset, int length, boolean isEnd)
114     throws IOException JavaDoc
115   {
116     long crc = _crc;
117
118     for (int i = 0; i < length; i++)
119       crc = Crc64.next(crc, buffer[offset + i]);
120
121     _crc = crc;
122     
123     _next.write(buffer, offset, length, isEnd);
124   }
125
126   /**
127    * Clears any buffered values in the write.
128    */

129   public void clearWrite()
130   {
131     _next.clearWrite();
132   }
133
134   /**
135    * Flushes the write output.
136    */

137   public void flush() throws IOException JavaDoc
138   {
139     _next.flush();
140   }
141
142   /**
143    * Closes the write output.
144    */

145   public void closeWrite() throws IOException JavaDoc
146   {
147     _next.closeWrite();
148   }
149
150   /**
151    * Returns the path.
152    */

153   public Path getPath()
154   {
155     return _next.getPath();
156   }
157
158   /**
159    * Sets the path.
160    */

161   public void setPath(Path path)
162   {
163     _next.setPath(path);
164   }
165
166   /**
167    * Closes the stream output.
168    */

169   public void close() throws IOException JavaDoc
170   {
171     _next.close();
172   }
173 }
174
Popular Tags