001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.randomaccess;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035: import java.util.Stack;
036:
037: /**
038: * Supports reading and writing to a random access data container. A random
039: * access data container behaves like a large array of bytes.
040: *
041: */
042: public abstract class AbstractRandomAccess implements IRandomAccess {
043: class MyOutputStream extends OutputStream {
044: public MyOutputStream() {
045: //
046: }
047:
048: /**
049: * must move with own pointer through random access!!
050: */
051: private long offset = 0;
052:
053: public void write(int b) throws IOException {
054: AbstractRandomAccess.this .seek(offset);
055: AbstractRandomAccess.this .write(b);
056: offset++;
057: }
058:
059: public void write(byte[] b) throws IOException {
060: AbstractRandomAccess.this .seek(offset);
061: AbstractRandomAccess.this .write(b);
062: offset += b.length;
063: }
064:
065: public void write(byte[] b, int off, int len)
066: throws IOException {
067: AbstractRandomAccess.this .seek(offset);
068: AbstractRandomAccess.this .write(b, off, len);
069: offset += len;
070: }
071:
072: public void close() throws IOException {
073: // do not close
074: }
075: }
076:
077: class MyInputStream extends InputStream {
078: public MyInputStream() {
079: //
080: }
081:
082: /**
083: * must move with own pointer through random access!!
084: */
085: private long offset = 0;
086:
087: public int read() throws IOException {
088: AbstractRandomAccess.this .seek(offset);
089: int i = AbstractRandomAccess.this .read();
090: if (i != -1) {
091: offset++;
092: }
093: return i;
094: }
095:
096: public int read(byte[] b, int off, int len) throws IOException {
097: AbstractRandomAccess.this .seek(offset);
098: int i = AbstractRandomAccess.this .read(b, off, len);
099: if (i != -1) {
100: offset += i;
101: }
102: return i;
103: }
104:
105: public int read(byte[] b) throws IOException {
106: AbstractRandomAccess.this .seek(offset);
107: int i = AbstractRandomAccess.this .read(b);
108: if (i != -1) {
109: offset += i;
110: }
111: return i;
112: }
113:
114: public void close() throws IOException {
115: // do not close....
116: }
117: }
118:
119: private Stack positionStack;
120:
121: public AbstractRandomAccess() {
122: positionStack = new Stack();
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see de.intarsys.tools.randomaccess.IRandomAccessData#getInputStream()
129: */
130: public InputStream asInputStream() {
131: return new MyInputStream();
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see de.intarsys.tools.randomaccess.IRandomAccessData#getOutputStream()
138: */
139: public OutputStream asOutputStream() {
140: return new MyOutputStream();
141: }
142:
143: /*
144: * (non-Javadoc)
145: *
146: * @see de.intarsys.tools.randomaccess.IRandomAccessData#mark()
147: */
148: public void mark() throws IOException {
149: getPositionStack().push(new Long(getOffset()));
150: }
151:
152: /*
153: * (non-Javadoc)
154: *
155: * @see de.intarsys.tools.randomaccess.IRandomAccessData#reset()
156: */
157: public void reset() throws IOException {
158: if (getPositionStack().isEmpty()) {
159: seek(0);
160: } else {
161: seek(((Long) getPositionStack().pop()).longValue());
162: }
163: }
164:
165: /**
166: * @return Returns the positionStack.
167: */
168: protected Stack getPositionStack() {
169: return positionStack;
170: }
171: }
|