001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: ByteArrayBuffer.java$
021: * $FileID: 4518$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:19 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.server.make;
032:
033: /**
034: * Title: SourceJammer 1.2
035: * Description:
036: * Copyright: Copyright (c) 2002
037: * Company: SourceJammer Project
038: * @author Robert MacGrogan
039: * @version 1.0
040: */
041:
042: import java.io.*;
043: import org.sourcejammer.util.FileUtil;
044:
045: public class ByteArrayBuffer {
046:
047: private byte[] buffer = null;
048: private FileInputStream stmIn = null;
049: private int bufferBeginIndex = -1;
050: private int bufferEndIndex = -1;
051: private int maxSize = -1;
052:
053: public ByteArrayBuffer(FileInputStream stmIn, int maxSize)
054: throws IOException {
055: this .stmIn = stmIn;
056: this .maxSize = maxSize;
057: bufferBeginIndex = 0;
058: readUpTo(maxSize - 1);
059: }
060:
061: public void readUpTo(int upToIndex) throws IOException {
062: //System.out.println("** up to: " + upToIndex);
063: if (upToIndex > bufferEndIndex) {
064: int iNewBeginIndex = upToIndex - (maxSize - 1);
065: int iBytesToKeep = bufferEndIndex - (iNewBeginIndex - 1);
066: int iBytesToGet = maxSize - iBytesToKeep;
067: //Check if we need to skip some bytes.
068: if (iBytesToKeep < 0) {
069: stmIn.skip((long) (-1 * iBytesToKeep));
070: iBytesToGet = maxSize;
071: }
072: //System.out.println("** getting: " + iBytesToGet);
073:
074: byte[] tmp = new byte[iBytesToGet];
075: int iNumBytesRead = stmIn.read(tmp);
076: if (iNumBytesRead == -1) {
077: throw new IOException(
078: "End of file encountered unexpectedly.");
079: } else if (iNumBytesRead < iBytesToGet) {
080: throw new IOException("More bytes read than expected.");
081: }
082:
083: buffer = getNewBuffer(iBytesToKeep, tmp);
084: bufferBeginIndex = iNewBeginIndex;
085: bufferEndIndex = upToIndex;
086: //System.out.println("** buffer size: " + buffer.length);
087: }
088: }
089:
090: public int sourceIndexToBufferIndex(int index) {
091: return index - bufferBeginIndex;
092: }
093:
094: public int bufferIndexToSourceIndex(int index) {
095: return index + bufferBeginIndex;
096: }
097:
098: public byte[] getBuffer() {
099: return buffer;
100: }
101:
102: public int beginIndex() {
103: return bufferBeginIndex;
104: }
105:
106: public int endIndex() {
107: return bufferEndIndex;
108: }
109:
110: protected void finalize() throws Throwable {
111: try {
112: close();
113: } catch (Throwable thr) {
114: super .finalize();
115: }
116: }
117:
118: public void close() throws IOException {
119: stmIn.close();
120: }
121:
122: private byte[] getNewBuffer(int numBytesToKeep, byte[] newBytes)
123: throws IOException {
124: byte[] newBuffer = new byte[numBytesToKeep + newBytes.length];
125: int iKeepFromIndex = buffer.length - numBytesToKeep;
126:
127: //Better test this.
128: System.arraycopy(buffer, iKeepFromIndex, newBuffer, 0,
129: numBytesToKeep);
130: System.arraycopy(newBytes, 0, newBuffer, numBytesToKeep,
131: newBuffer.length);
132: return newBuffer;
133: }
134:
135: }
|