1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

tx => notx

This commit is contained in:
Nikolay Pultsin 2012-04-06 18:07:18 +01:00
parent 46d3762b53
commit d595280b77

View file

@ -1,155 +1,155 @@
package org.amse.ys.zip; package org.amse.ys.zip;
import java.io.*; import java.io.*;
final class MyBufferedInputStream extends InputStream { final class MyBufferedInputStream extends InputStream {
private final ZipFile.InputStreamHolder myStreamHolder; private final ZipFile.InputStreamHolder myStreamHolder;
private InputStream myFileInputStream; private InputStream myFileInputStream;
private final byte[] myBuffer; private final byte[] myBuffer;
int myBytesReady; int myBytesReady;
int myPositionInBuffer; int myPositionInBuffer;
private int myCurrentPosition; private int myCurrentPosition;
public MyBufferedInputStream(ZipFile.InputStreamHolder streamHolder, int bufferSize) throws IOException { public MyBufferedInputStream(ZipFile.InputStreamHolder streamHolder, int bufferSize) throws IOException {
myStreamHolder = streamHolder; myStreamHolder = streamHolder;
myFileInputStream = streamHolder.getInputStream(); myFileInputStream = streamHolder.getInputStream();
myBuffer = new byte[bufferSize]; myBuffer = new byte[bufferSize];
myBytesReady = 0; myBytesReady = 0;
myPositionInBuffer = 0; myPositionInBuffer = 0;
} }
public MyBufferedInputStream(ZipFile.InputStreamHolder streamHolder) throws IOException { public MyBufferedInputStream(ZipFile.InputStreamHolder streamHolder) throws IOException {
this(streamHolder, 1 << 10); this(streamHolder, 1 << 10);
} }
public int available() throws IOException { public int available() throws IOException {
return (myFileInputStream.available() + myBytesReady); return (myFileInputStream.available() + myBytesReady);
} }
int offset() { int offset() {
return myCurrentPosition; return myCurrentPosition;
} }
public int read(byte[] b, int off, int len) throws IOException { public int read(byte[] b, int off, int len) throws IOException {
int ready = (len < myBytesReady) ? len : myBytesReady; int ready = (len < myBytesReady) ? len : myBytesReady;
if (ready > 0) { if (ready > 0) {
System.arraycopy(myBuffer, myPositionInBuffer, b, off, ready); System.arraycopy(myBuffer, myPositionInBuffer, b, off, ready);
len -= ready; len -= ready;
myBytesReady -= ready; myBytesReady -= ready;
myPositionInBuffer += ready; myPositionInBuffer += ready;
off += ready; off += ready;
} }
if (len > 0) { if (len > 0) {
final int ready2 = myFileInputStream.read(b, off, len); final int ready2 = myFileInputStream.read(b, off, len);
if (ready2 >= 0) { if (ready2 >= 0) {
ready += ready2; ready += ready2;
} }
} }
myCurrentPosition += ready; myCurrentPosition += ready;
return (ready > 0) ? ready : -1; return (ready > 0) ? ready : -1;
} }
public int read() throws IOException { public int read() throws IOException {
myCurrentPosition++; myCurrentPosition++;
if (myBytesReady <= 0) { if (myBytesReady <= 0) {
myPositionInBuffer = 0; myPositionInBuffer = 0;
myBytesReady = myFileInputStream.read(myBuffer); myBytesReady = myFileInputStream.read(myBuffer);
if (myBytesReady <= 0) { if (myBytesReady <= 0) {
return -1; return -1;
} }
} }
myBytesReady--; myBytesReady--;
return myBuffer[myPositionInBuffer++] & 255; return myBuffer[myPositionInBuffer++] & 255;
} }
int read2Bytes() throws IOException { int read2Bytes() throws IOException {
int low = read(); int low = read();
int high = read(); int high = read();
if (high < 0) { if (high < 0) {
throw new ZipException("unexpected end of file at position " + offset()); throw new ZipException("unexpected end of file at position " + offset());
} }
return (high << 8) + low; return (high << 8) + low;
} }
int read4Bytes() throws IOException { int read4Bytes() throws IOException {
int firstByte = read(); int firstByte = read();
int secondByte = read(); int secondByte = read();
int thirdByte = read(); int thirdByte = read();
int fourthByte = read(); int fourthByte = read();
if (fourthByte < 0) { if (fourthByte < 0) {
throw new ZipException("unexpected end of file at position " + offset()); throw new ZipException("unexpected end of file at position " + offset());
} }
return (fourthByte << 24) + (thirdByte << 16) + (secondByte << 8) + firstByte; return (fourthByte << 24) + (thirdByte << 16) + (secondByte << 8) + firstByte;
} }
String readString(int stringLength) throws IOException { String readString(int stringLength) throws IOException {
char[] array = new char[stringLength]; char[] array = new char[stringLength];
for (int i = 0; i < stringLength; i++) { for (int i = 0; i < stringLength; i++) {
array[i] = (char)read(); array[i] = (char)read();
} }
return new String(array); return new String(array);
} }
@Override @Override
public long skip(long n) throws IOException { public long skip(long n) throws IOException {
if (myBytesReady >= n) { if (myBytesReady >= n) {
myBytesReady -= n; myBytesReady -= n;
myPositionInBuffer += n; myPositionInBuffer += n;
myCurrentPosition += n; myCurrentPosition += n;
return n; return n;
} else { } else {
long left = n - myBytesReady; long left = n - myBytesReady;
myBytesReady = 0; myBytesReady = 0;
left -= myFileInputStream.skip(left); left -= myFileInputStream.skip(left);
while (left > 0) { while (left > 0) {
int skipped = myFileInputStream.read(myBuffer, 0, Math.min((int)left, myBuffer.length)); int skipped = myFileInputStream.read(myBuffer, 0, Math.min((int)left, myBuffer.length));
if (skipped <= 0) { if (skipped <= 0) {
break; break;
} }
left -= skipped; left -= skipped;
} }
return n - left; return n - left;
} }
} }
public void backSkip(int n) throws IOException { public void backSkip(int n) throws IOException {
if (n <= 0) { if (n <= 0) {
return; return;
} }
myFileInputStream.close(); myFileInputStream.close();
myFileInputStream = myStreamHolder.getInputStream(); myFileInputStream = myStreamHolder.getInputStream();
myBytesReady = 0; myBytesReady = 0;
myPositionInBuffer = 0; myPositionInBuffer = 0;
int position = myCurrentPosition - n; int position = myCurrentPosition - n;
myCurrentPosition = 0; myCurrentPosition = 0;
skip(position); skip(position);
} }
public void setPosition(int position) throws IOException { public void setPosition(int position) throws IOException {
if (myCurrentPosition < position) { if (myCurrentPosition < position) {
skip(position - myCurrentPosition); skip(position - myCurrentPosition);
} else { } else {
backSkip(myCurrentPosition - position); backSkip(myCurrentPosition - position);
} }
} }
/* /*
public void setPosition(int position) throws IOException { public void setPosition(int position) throws IOException {
if (myCurrentPosition < position) { if (myCurrentPosition < position) {
skip(position - myCurrentPosition); skip(position - myCurrentPosition);
} else { } else {
myFileInputStream.close(); myFileInputStream.close();
myFileInputStream = myStreamHolder.getInputStream(); myFileInputStream = myStreamHolder.getInputStream();
myBytesReady = 0; myBytesReady = 0;
skip(position); skip(position);
myCurrentPosition = position; myCurrentPosition = position;
} }
} }
*/ */
public void close() throws IOException { public void close() throws IOException {
myFileInputStream.close(); myFileInputStream.close();
myBytesReady = 0; myBytesReady = 0;
} }
} }