001: /*
002: * $Id: BufferedDataOutputStream.java,v 1.4 2005/08/24 00:40:56 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.io;
042:
043: import java.io.BufferedOutputStream;
044: import java.io.DataOutputStream;
045: import java.io.FilterOutputStream;
046: import java.io.IOException;
047:
048: /**
049: * Utility that wraps a {@link AxionOutputStream}in a {@link DataOutputStream}and
050: * buffers output through a {@link BufferedOutputStream}.
051: *
052: * @version $Revision: 1.4 $ $Date: 2005/08/24 00:40:56 $
053: * @author Ahimanikya Satapathy
054: */
055: public class BufferedDataOutputStream extends DataOutputStream {
056:
057: private static class Buffer extends BufferedOutputStream {
058: private int position;
059:
060: public Buffer(PositionCache out, int bufferSize)
061: throws IOException {
062: super (out, bufferSize);
063: }
064:
065: public void flush() throws IOException {
066: super .flush();
067: position = 0; // invalidate the buffer
068: }
069:
070: public long getPosition() throws IOException {
071: return ((PositionCache) out).getPos() + this .position;
072: }
073:
074: public void seek(long desired) throws IOException {
075: long start = ((PositionCache) out).getPos();
076: long current = start + this .position;
077: if (desired >= start && desired < start + buf.length) {
078: this .position += (desired - current); // can position within buffer
079: } else {
080: if (count > 0) {
081: flush(); // flush the buffer
082: }
083: ((PositionCache) out).seek(desired); // seek underlying stream
084: }
085: }
086:
087: public void write(byte b[], int off, int len)
088: throws IOException {
089: if (len >= buf.length) {
090: flush();
091: out.write(b, off, len);
092: this .position = 0;
093: return;
094: }
095:
096: if (len > buf.length - count) {
097: flush();
098: this .position = 0;
099: }
100:
101: System.arraycopy(b, off, buf, position, len);
102: position += len;
103: if (position > count) {
104: count = position;
105: }
106: }
107:
108: // optimized version of write(int)
109: public void write(int b) throws IOException {
110: if (count >= buf.length) {
111: super .write(b);
112: this .position = 1;
113: } else {
114: buf[position++] = (byte) b;
115: if (position > count) {
116: count = position;
117: }
118: }
119: }
120:
121: }
122:
123: private static class PositionCache extends FilterOutputStream {
124: long position;
125:
126: public PositionCache(AxionOutputStream out) throws IOException {
127: super (out);
128: this .position = out.getPos();
129: }
130:
131: public long getPos() throws IOException {
132: return position; // return cached position
133: }
134:
135: public void seek(long pos) throws IOException {
136: ((AxionOutputStream) out).seek(pos);
137: position = pos;
138: }
139:
140: // This is the only write() method called by BufferedOutputStream, so we
141: // trap calls to it in order to cache the position.
142: public void write(byte b[], int off, int len)
143: throws IOException {
144: out.write(b, off, len);
145: position += len; // update position
146: }
147: }
148:
149: public BufferedDataOutputStream(AxionOutputStream out)
150: throws IOException {
151: this (out, 512);
152: }
153:
154: public BufferedDataOutputStream(AxionOutputStream out,
155: int bufferSize) throws IOException {
156: super (new Buffer(new PositionCache(out), bufferSize));
157: }
158:
159: public long getPos() throws IOException {
160: return ((Buffer) out).getPosition();
161: }
162:
163: public void seek(long pos) throws IOException {
164: ((Buffer) out).seek(pos);
165: }
166:
167: }
|