2015-05-27 13:13:19 +00:00
|
|
|
/*
|
2015-10-01 15:23:40 +00:00
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2015 Ziver Koc
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
2015-05-27 13:13:19 +00:00
|
|
|
*/
|
2013-05-28 19:29:24 +00:00
|
|
|
|
2010-08-13 22:27:55 +00:00
|
|
|
package zutil.io;
|
2008-11-14 16:38:36 +00:00
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.RandomAccessFile;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class is a buffer for the RandomeAccesFile
|
|
|
|
|
* Inspiration:
|
|
|
|
|
* http://www.javaworld.com/javaworld/javatips/jw-javatip26.html
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
|
|
|
|
public class BufferedRandomAccessFile extends RandomAccessFile{
|
2010-08-14 15:22:02 +00:00
|
|
|
// The size of the buffer in Byte
|
|
|
|
|
private int BUF_SIZE = 64*1024;
|
2008-11-14 16:38:36 +00:00
|
|
|
|
|
|
|
|
// The Buffer
|
|
|
|
|
byte buffer[];
|
|
|
|
|
// The end of the buffer
|
|
|
|
|
int buf_end = 0;
|
|
|
|
|
// The position in the buffer
|
|
|
|
|
int buf_pos = 0;
|
|
|
|
|
// The real file pointer position where the buffer starts
|
2010-08-13 22:27:55 +00:00
|
|
|
long file_pos = 0;
|
2008-11-14 16:38:36 +00:00
|
|
|
|
2010-08-13 22:27:55 +00:00
|
|
|
/**
|
|
|
|
|
* Create a instance of this buffer
|
|
|
|
|
*
|
|
|
|
|
* @param filename is the file to read from
|
|
|
|
|
* @param mode as in {@link java.io.RandomAccessFile#RandomAccessFile(File file, String mode)}
|
|
|
|
|
*/
|
2008-11-14 16:38:36 +00:00
|
|
|
public BufferedRandomAccessFile(String filename, String mode) throws IOException{
|
|
|
|
|
this(new File(filename), mode);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 22:27:55 +00:00
|
|
|
/**
|
|
|
|
|
* Create a instance of this buffer
|
|
|
|
|
*
|
|
|
|
|
* @param file is the file to read from
|
|
|
|
|
* @param mode as in {@link java.io.RandomAccessFile#RandomAccessFile(File file, String mode)}
|
|
|
|
|
*/
|
2008-11-14 16:38:36 +00:00
|
|
|
public BufferedRandomAccessFile(File file, String mode) throws IOException{
|
|
|
|
|
super(file,mode);
|
|
|
|
|
invalidate();
|
|
|
|
|
buffer = new byte[BUF_SIZE];
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 22:27:55 +00:00
|
|
|
/**
|
|
|
|
|
* Create a instance of this buffer
|
|
|
|
|
*
|
|
|
|
|
* @param filename is the file to read from
|
|
|
|
|
* @param mode as in {@link java.io.RandomAccessFile#RandomAccessFile(File file, String mode)}
|
|
|
|
|
* @param bufsize is the buffer size in bytes
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
2008-11-14 16:38:36 +00:00
|
|
|
public BufferedRandomAccessFile(String filename, String mode, int bufsize) throws IOException{
|
|
|
|
|
this(new File(filename), mode, bufsize);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 22:27:55 +00:00
|
|
|
/**
|
|
|
|
|
* Create a instance of this buffer
|
|
|
|
|
*
|
|
|
|
|
* @param file is the file to read from
|
|
|
|
|
* @param mode as in {@link java.io.RandomAccessFile#RandomAccessFile(File file, String mode)}
|
|
|
|
|
* @param bufsize is the buffer size in bytes
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
2008-11-14 16:38:36 +00:00
|
|
|
public BufferedRandomAccessFile(File file, String mode, int bufsize) throws IOException{
|
|
|
|
|
super(file,mode);
|
|
|
|
|
invalidate();
|
|
|
|
|
BUF_SIZE = bufsize;
|
2010-08-13 22:27:55 +00:00
|
|
|
buffer = new byte[BUF_SIZE];
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads in data from the file to the buffer
|
|
|
|
|
*
|
2010-08-13 22:27:55 +00:00
|
|
|
* @return the buffer
|
2008-11-14 16:38:36 +00:00
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private int fillBuffer() throws IOException {
|
|
|
|
|
int n = super.read(buffer, 0, BUF_SIZE );
|
|
|
|
|
if(n >= 0) {
|
2010-08-13 22:27:55 +00:00
|
|
|
file_pos +=n;
|
2008-11-14 16:38:36 +00:00
|
|
|
buf_end = n;
|
|
|
|
|
buf_pos = 0;
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the buffer
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private void invalidate() throws IOException {
|
|
|
|
|
buf_end = 0;
|
|
|
|
|
buf_pos = 0;
|
2010-08-13 22:27:55 +00:00
|
|
|
file_pos = super.getFilePointer();
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-14 15:22:02 +00:00
|
|
|
/**
|
|
|
|
|
* @return the next byte in the buffer
|
|
|
|
|
*/
|
|
|
|
|
public final int read() throws IOException{
|
|
|
|
|
if(buf_pos >= buf_end) {
|
|
|
|
|
if(fillBuffer() < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if(buf_end == 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
buf_pos++;
|
|
|
|
|
return buffer[buf_pos-1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
/**
|
2010-08-13 22:27:55 +00:00
|
|
|
* Fills the given array with data from the buffer
|
|
|
|
|
*
|
|
|
|
|
* @param b is the array that will be filled
|
|
|
|
|
* @return the amount of bytes read or -1 if eof
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public int read(byte b[]) throws IOException {
|
|
|
|
|
return read(b, 0, b.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads a given length of bytes from the buffer
|
|
|
|
|
*
|
2010-08-13 22:27:55 +00:00
|
|
|
* @param b is the array that will be filled
|
|
|
|
|
* @param off is the offset in the array
|
|
|
|
|
* @param len is the amount to read
|
|
|
|
|
* @return the amount of bytes read or -1 if eof
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public int read(byte b[], int off, int len) throws IOException {
|
2010-08-14 15:22:02 +00:00
|
|
|
if(buf_pos >= buf_end) {
|
|
|
|
|
if(fillBuffer() < 0)
|
|
|
|
|
return -1; // EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy from buffer
|
2008-11-14 16:38:36 +00:00
|
|
|
int leftover = buf_end - buf_pos;
|
|
|
|
|
if(len <= leftover) {
|
|
|
|
|
System.arraycopy(buffer, buf_pos, b, off, len);
|
|
|
|
|
buf_pos += len;
|
|
|
|
|
return len;
|
|
|
|
|
}
|
2010-08-14 15:22:02 +00:00
|
|
|
|
|
|
|
|
System.arraycopy(buffer, buf_pos, b, off, leftover);
|
|
|
|
|
int n = super.read(b, off+leftover, len-leftover );
|
|
|
|
|
fillBuffer();
|
|
|
|
|
if( n >= 0 )
|
|
|
|
|
return leftover + n;
|
|
|
|
|
return leftover;
|
|
|
|
|
/*for(int i = 0; i < len; i++) {
|
2008-11-14 16:38:36 +00:00
|
|
|
int c = this.read();
|
|
|
|
|
if(c != -1)
|
|
|
|
|
b[off+i] = (byte)c;
|
|
|
|
|
else {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-08-14 15:22:02 +00:00
|
|
|
return len;*/
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-08-13 22:27:55 +00:00
|
|
|
* @return the file pointer in the file
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public long getFilePointer() throws IOException{
|
2010-08-13 22:27:55 +00:00
|
|
|
long l = file_pos;
|
2008-11-14 16:38:36 +00:00
|
|
|
return (l - buf_end + buf_pos) ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the file pointer to another position
|
|
|
|
|
*
|
|
|
|
|
* @param pos The position to move the pointer to
|
|
|
|
|
*/
|
|
|
|
|
public void seek(long pos) throws IOException {
|
2010-08-13 22:27:55 +00:00
|
|
|
int n = (int)(file_pos - pos);
|
2008-11-14 16:38:36 +00:00
|
|
|
if(n >= 0 && n <= buf_end) {
|
|
|
|
|
buf_pos = buf_end - n;
|
|
|
|
|
} else {
|
|
|
|
|
super.seek(pos);
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method is a replacement for readLine()
|
2010-08-13 22:27:55 +00:00
|
|
|
*
|
|
|
|
|
* @return the next line in the file
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public final String readNextLine() throws IOException {
|
|
|
|
|
String str = null;
|
|
|
|
|
if(buf_end-buf_pos <= 0) {
|
|
|
|
|
if(fillBuffer() < 0) {
|
|
|
|
|
throw new IOException("Error filling buffer!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int lineend = -1;
|
|
|
|
|
for(int i = buf_pos; i < buf_end; i++) {
|
|
|
|
|
if(buffer[i] == '\n') {
|
|
|
|
|
lineend = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(lineend < 0) {
|
|
|
|
|
StringBuffer input = new StringBuffer(256);
|
|
|
|
|
int c;
|
|
|
|
|
while (((c = read()) != -1) && (c != '\n')) {
|
|
|
|
|
input.append((char)c);
|
|
|
|
|
}
|
|
|
|
|
if ((c == -1) && (input.length() == 0)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return input.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(lineend > 0 && buffer[lineend-1] == '\r'){
|
|
|
|
|
str = new String(buffer, buf_pos, lineend - buf_pos -1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
str = new String(buffer, buf_pos, lineend - buf_pos);
|
|
|
|
|
}
|
|
|
|
|
buf_pos = lineend +1;
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|