| 1 |
// Copyright 2001, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
|
| 7 |
/** |
| 8 |
* The input buffer can be limited to less than the number of bytes of the |
| 9 |
* underlying buffer. Only one real input stream exists, which is where the |
| 10 |
* reads take place. A buffer is limited by some length. If more is read, -1 is |
| 11 |
* returned. Multiple limits can be set by calling pushBuffer. If bytes are left |
| 12 |
* in the buffer when popBuffer is called, they are returned in an array. |
| 13 |
* Otherwise null is returned. |
| 14 |
* |
| 15 |
* @author Mark Donszelmann |
| 16 |
* @author Charles Loomis |
| 17 |
* @version $Id: ByteCountInputStream.java 8584 2006-08-10 23:06:37Z duns $ |
| 18 |
*/ |
| 19 |
public class ByteCountInputStream extends ByteOrderInputStream { |
| 20 |
|
| 21 |
private int index; |
| 22 |
|
| 23 |
private int[] size; |
| 24 |
|
| 25 |
private long len; |
| 26 |
|
| 27 |
/** |
| 28 |
* Create a Byte Count input stream from given stream |
| 29 |
* |
| 30 |
* @param in stream to read from |
| 31 |
* @param littleEndian true if stream should be little endian |
| 32 |
* @param stackDepth maximum number of buffers used while reading |
| 33 |
*/ |
| 34 |
public ByteCountInputStream(InputStream in, boolean littleEndian, |
| 35 |
int stackDepth) { |
| 36 |
super(in, littleEndian); |
| 37 |
size = new int[stackDepth]; |
| 38 |
index = -1; |
| 39 |
len = 0; |
| 40 |
} |
| 41 |
|
| 42 |
public int read() throws IOException { |
| 43 |
// original stream |
| 44 |
if (index == -1) { |
| 45 |
len++; |
| 46 |
return super.read(); |
| 47 |
} |
| 48 |
|
| 49 |
// end of buffer |
| 50 |
if (size[index] <= 0) |
| 51 |
return -1; |
| 52 |
|
| 53 |
// decrease counter |
| 54 |
size[index]--; |
| 55 |
|
| 56 |
len++; |
| 57 |
return super.read(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Push the current buffer to the stack |
| 62 |
* |
| 63 |
* @param len number of bytes that can be read from the current buffer |
| 64 |
*/ |
| 65 |
public void pushBuffer(int len) { |
| 66 |
if (index >= size.length - 1) { |
| 67 |
System.err |
| 68 |
.println("ByteCountInputStream: trying to push more buffers than stackDepth: " |
| 69 |
+ size.length); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (index >= 0) { |
| 74 |
if (size[index] < len) { |
| 75 |
System.err |
| 76 |
.println("ByteCountInputStream: trying to set a length: " |
| 77 |
+ len |
| 78 |
+ ", longer than the underlying buffer: " |
| 79 |
+ size[index]); |
| 80 |
return; |
| 81 |
} |
| 82 |
size[index] -= len; |
| 83 |
} |
| 84 |
index++; |
| 85 |
size[index] = len; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Pops the buffer from the stack and returns leftover bytes in a byte array |
| 90 |
* |
| 91 |
* @return null if buffer was completely read. Otherwise rest of buffer is |
| 92 |
* read and returned. |
| 93 |
* @throws IOException if read fails |
| 94 |
*/ |
| 95 |
public byte[] popBuffer() throws IOException { |
| 96 |
if (index >= 0) { |
| 97 |
int len = size[index]; |
| 98 |
if (len > 0) { |
| 99 |
return readByte(len); |
| 100 |
} else if (len < 0) { |
| 101 |
System.err.println("ByteCountInputStream: Internal Error"); |
| 102 |
} |
| 103 |
index--; |
| 104 |
} |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @return number of bytes that can be read from the current buffer |
| 110 |
*/ |
| 111 |
public long getLength() { |
| 112 |
return (index >= 0) ? size[index] : len; |
| 113 |
} |
| 114 |
} |