de.uniks.networkparser.ext.io.FileBuffer Maven / Gradle / Ivy
package de.uniks.networkparser.ext.io;
/*
NetworkParser
The MIT License
Copyright (c) 2010-2016 Stefan Lindel https://github.com/fujaba/NetworkParser/
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import de.uniks.networkparser.buffer.Buffer;
import de.uniks.networkparser.buffer.CharacterBuffer;
public class FileBuffer extends Buffer {
private BufferedReader reader;
private File file;
private CharacterBuffer lookAHead = new CharacterBuffer();
private int length;
private char currentChar;
public FileBuffer withFile(String fileName) {
withFile(new File(fileName));
return this;
}
public FileBuffer withFile(File file, int cache) {
this.file = file;
this.length = (int) this.file.length();
try {
FileInputStream fis = new FileInputStream(this.file);
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
this.reader = new BufferedReader(isr, cache);
}catch (Exception e) {
}
this.position = 0;
return this;
}
public FileBuffer withFile(File file) {
return withFile(file, 1024*1024);
}
@Override
public int length() {
return length;
}
public boolean exist() {
if(this.file == null) {
return false;
}
return this.file.exists();
}
@Override
public char getChar() {
char value = 0;
if(lookAHead.length() > 0) {
value = lookAHead.charAt(0);
if(lookAHead.length() == 1) {
lookAHead.clear();
}else {
lookAHead.addStart(1);
}
this.position++;
return value;
}
try {
value = (char) this.reader.read();
this.currentChar = value;
position++;
} catch (IOException e) {
}
return value;
}
@Override
public String toString() {
char[] values = new char[remaining()];
int len = lookAHead.length();
if(len>0) {
for(int i = 0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy