Update of /cvsroot/archive-crawler/ArchiveOpenCrawler/src/org/archive/crawler/io In directory sc8-pr-cvs1:/tmp/cvs-serv4243/src/org/archive/crawler/io Modified Files: RecordingOutputStream.java Added Files: RecordingInputStream.java ReplayInputStream.java Log Message: http byte-level recording (in progress) --- NEW FILE: RecordingInputStream.java --- /* * RecordingInputStream.java * Created on Sep 24, 2003 * * $Header: /cvsroot/archive-crawler/ArchiveOpenCrawler/src/org/archive/crawler/io/RecordingInputStream.java,v 1.1 2003/09/25 00:14:03 gojomo Exp $ */ package org.archive.crawler.io; import java.io.IOException; import java.io.InputStream; import org.archive.util.NullOutputStream; /** * @author gojomo * */ public class RecordingInputStream extends InputStream { protected InputStream wrappedStream; protected RecordingOutputStream recordingOutputStream; /** * Create a new RecordingInputStream with the specified parameters. * * @param bufferSize * @param backingFile * @param maxSize */ public RecordingInputStream(int bufferSize, String backingFilename, int maxSize) { recordingOutputStream = new RecordingOutputStream(bufferSize, backingFilename, maxSize); } public void open(InputStream wrappedStream) throws IOException { this.wrappedStream = wrappedStream; recordingOutputStream.open(new NullOutputStream()); } /* (non-Javadoc) * @see java.io.InputStream#read() */ public int read() throws IOException { int b = wrappedStream.read(); recordingOutputStream.write(b); return b; } /* (non-Javadoc) * @see java.io.OutputStream#close() */ public void close() throws IOException { super.close(); wrappedStream.close(); recordingOutputStream.close(); } public ReplayInputStream getReplayInputStream() throws IOException { return recordingOutputStream.getReplayInputStream(); } public long readFully() throws IOException { while(read()!=-1) { } return recordingOutputStream.size; } } --- NEW FILE: ReplayInputStream.java --- /* * ReplayInputStream.java * Created on Sep 24, 2003 * * $Header: /cvsroot/archive-crawler/ArchiveOpenCrawler/src/org/archive/crawler/io/ReplayInputStream.java,v 1.1 2003/09/25 00:14:03 gojomo Exp $ */ package org.archive.crawler.io; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; /** * @author gojomo * */ public class ReplayInputStream extends InputStream { private BufferedInputStream diskStream; byte[] buffer; long size; long position; String backingFilename; /** * @param buffer * @param size * @param backingFilename */ public ReplayInputStream(byte[] buffer, long size, String backingFilename) throws IOException { this.buffer = buffer; this.size = size; if (size>buffer.length) { this.backingFilename = backingFilename; diskStream = new BufferedInputStream(new FileInputStream(backingFilename),4096); } } /* (non-Javadoc) * @see java.io.InputStream#read() */ public int read() throws IOException { if (position<buffer.length) { return buffer[(int)position++]; } else { position++; return diskStream.read(); } } // TODO: implement other read()s for efficiency } Index: RecordingOutputStream.java =================================================================== RCS file: /cvsroot/archive-crawler/ArchiveOpenCrawler/src/org/archive/crawler/io/RecordingOutputStream.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RecordingOutputStream.java 24 Sep 2003 01:46:37 -0000 1.1 --- RecordingOutputStream.java 25 Sep 2003 00:14:03 -0000 1.2 *************** *** 7,18 **** --- 7,71 ---- package org.archive.crawler.io; + import java.io.BufferedOutputStream; + import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** + * A RecordingOutputStream can be wrapped around any other + * OutputStream to record all bytes written to it. You can + * then request a ReplayInputStream to read those bytes. + * + * The RecordingOutputStream uses an in-memory buffer and + * backing disk file to allow it to record streams of + * arbitrary length, limited only by available disk space. + * + * As long as the stream recorded is smaller than the + * in-memory buffer, no disk access will occur. + * * @author gojomo * */ public class RecordingOutputStream extends OutputStream { + protected long size; + protected int maxSize; + protected String backingFilename; + protected BufferedOutputStream diskStream; + protected OutputStream wrappedStream; + protected byte[] buffer; + protected long position; + + /** + * Create a new RecordingInputStream with the specified parameters. + * + * @param bufferSize + * @param backingFile + * @param maxSize + */ + public RecordingOutputStream(int bufferSize, String backingFilename, int maxSize) { + buffer = new byte[bufferSize]; + this.backingFilename = backingFilename; + this.maxSize = maxSize; + } + + + public void open(OutputStream wrappedStream) throws IOException { + this.wrappedStream = wrappedStream; + this.position = 0; + diskStream = new BufferedOutputStream(new FileOutputStream(backingFilename),4096); + } + + /** + * Total reset -- discarding all + */ + public void clear() { + try { + diskStream.close(); + } catch (IOException e) { + // nothing + } + diskStream = null; + } + /* (non-Javadoc) *************** *** 20,25 **** */ public void write(int b) throws IOException { ! // TODO Auto-generated method stub } --- 73,116 ---- */ public void write(int b) throws IOException { ! wrappedStream.write(b); ! record(b); ! } ! ! // TODO implement other forms of write() for efficiency ! ! /** ! * @param b ! */ ! private void record(int b) throws IOException { ! if(position>buffer.length){ ! diskStream.write(b); ! } else { ! buffer[(int)position] = (byte)b; ! } ! position++; ! } ! ! // TODO implement other forms of record() for efficiency + /* (non-Javadoc) + * @see java.io.OutputStream#close() + */ + public void close() throws IOException { + super.close(); + wrappedStream.close(); + diskStream.close(); + this.size = position; + } + + /* (non-Javadoc) + * @see java.io.OutputStream#flush() + */ + public void flush() throws IOException { + super.flush(); + wrappedStream.flush(); + } + + public ReplayInputStream getReplayInputStream() throws IOException { + return new ReplayInputStream(buffer,size,backingFilename); } |