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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035: import java.util.logging.Logger;
036:
037: /**
038: * Reads from a file in a random-access fashion.
039: */
040: public class SpyRandomAccessStream extends RandomAccessStream {
041: private static final Logger log = Logger
042: .getLogger(SpyRandomAccessStream.class.getName());
043:
044: private RandomAccessStream _file;
045:
046: public SpyRandomAccessStream(RandomAccessStream file) {
047: _file = file;
048: }
049:
050: /**
051: * Returns the length.
052: */
053: public long getLength() throws IOException {
054: return _file.getLength();
055: }
056:
057: /**
058: * Reads a block from a given location.
059: */
060: public int read(byte[] buffer, int offset, int length)
061: throws IOException {
062: log.finest("random-read(0x"
063: + Long.toHexString(getFilePointer()) + "," + length
064: + ")");
065:
066: return _file.read(buffer, offset, length);
067: }
068:
069: /**
070: * Reads a block from a given location.
071: */
072: public int read(char[] buffer, int offset, int length)
073: throws IOException {
074: log.finest("random-read(0x"
075: + Long.toHexString(getFilePointer()) + "," + length
076: + ")");
077:
078: return _file.read(buffer, offset, length);
079: }
080:
081: /**
082: * Reads a block from a given location.
083: */
084: public int read(long fileOffset, byte[] buffer, int offset,
085: int length) throws IOException {
086: log.info("random-read(0x" + Long.toHexString(fileOffset) + ","
087: + length + ")");
088:
089: return _file.read(fileOffset, buffer, offset, length);
090: }
091:
092: public void write(byte[] buffer, int offset, int length)
093: throws IOException {
094: log.info("random-write(0x" + Long.toHexString(getFilePointer())
095: + "," + length + ")");
096:
097: _file.write(buffer, offset, length);
098: }
099:
100: /**
101: * Writes a block from a given location.
102: */
103: public void write(long fileOffset, byte[] buffer, int offset,
104: int length) throws IOException {
105: log.info("random-write(0x" + Long.toHexString(fileOffset) + ","
106: + length + ")");
107:
108: _file.write(fileOffset, buffer, offset, length);
109: }
110:
111: /**
112: * Seeks to the given position in the file.
113: */
114: public boolean seek(long position) {
115: log.info("random-seek(0x" + position + ")");
116:
117: return _file.seek(position);
118: }
119:
120: /**
121: * Returns an OutputStream for this stream.
122: */
123: public OutputStream getOutputStream() throws IOException {
124: return _file.getOutputStream();
125: }
126:
127: /**
128: * Returns an InputStream for this stream.
129: */
130: public InputStream getInputStream() throws IOException {
131: return _file.getInputStream();
132: }
133:
134: /**
135: * Read a byte from the file, advancing the pointer.
136: */
137: public int read() throws IOException {
138: log.info("random-read(0x" + Long.toHexString(getFilePointer())
139: + ",1)");
140:
141: return _file.read();
142: }
143:
144: /**
145: * Write a byte to the file, advancing the pointer.
146: */
147: public void write(int b) throws IOException {
148: log.info("random-write(0x" + Long.toHexString(getFilePointer())
149: + ",1)");
150:
151: _file.write(b);
152: }
153:
154: /**
155: * Returns the current position of the file pointer.
156: */
157: public long getFilePointer() throws IOException {
158: return _file.getFilePointer();
159: }
160:
161: /**
162: * Closes the stream.
163: */
164: public void close() throws IOException {
165: _file.close();
166: }
167: }
|