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 com.caucho.util.Crc64;
032:
033: import java.io.IOException;
034:
035: public class Crc64Stream extends StreamImpl {
036: private StreamImpl _next;
037: private long _crc;
038:
039: public Crc64Stream(StreamImpl next) {
040: _next = next;
041: }
042:
043: public Crc64Stream() {
044: }
045:
046: /**
047: * Initialize the filter with a new stream.
048: */
049: public void init(StreamImpl next) {
050: _next = next;
051: _crc = 0;
052: }
053:
054: /**
055: * Returns the CRC value.
056: */
057: public long getCRC() {
058: return _crc;
059: }
060:
061: /**
062: * Returns true if the stream can read.
063: */
064: public boolean canRead() {
065: return _next.canRead();
066: }
067:
068: /**
069: * Reads a buffer from the underlying stream.
070: *
071: * @param buffer the byte array to read.
072: * @param offset the offset into the byte array.
073: * @param length the number of bytes to read.
074: */
075: public int read(byte[] buffer, int offset, int length)
076: throws IOException {
077: int len = _next.read(buffer, offset, length);
078:
079: _crc = Crc64.generate(_crc, buffer, offset, len);
080:
081: return len;
082: }
083:
084: /**
085: * Returns true if the stream can write.
086: */
087: public boolean canWrite() {
088: return _next.canWrite();
089: }
090:
091: /**
092: * Writes a buffer to the underlying stream.
093: *
094: * @param buffer the byte array to write.
095: * @param offset the offset into the byte array.
096: * @param length the number of bytes to write.
097: * @param isEnd true when the write is flushing a close.
098: */
099: public void write(byte[] buffer, int offset, int length,
100: boolean isEnd) throws IOException {
101: _crc = Crc64.generate(_crc, buffer, offset, length);
102:
103: _next.write(buffer, offset, length, isEnd);
104: }
105:
106: /**
107: * Clears any buffered values in the write.
108: */
109: public void clearWrite() {
110: _next.clearWrite();
111: }
112:
113: /**
114: * Flushes the write output.
115: */
116: public void flush() throws IOException {
117: _next.flush();
118: }
119:
120: /**
121: * Closes the write output.
122: */
123: public void closeWrite() throws IOException {
124: _next.closeWrite();
125: }
126:
127: /**
128: * Returns the path.
129: */
130: public Path getPath() {
131: return _next.getPath();
132: }
133:
134: /**
135: * Sets the path.
136: */
137: public void setPath(Path path) {
138: _next.setPath(path);
139: }
140:
141: /**
142: * Closes the stream output.
143: */
144: public void close() throws IOException {
145: _next.close();
146: }
147: }
|