package com.tail;
import java.io.BufferedOutputStream;
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.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class Reader {
//Tama�o del vector que almacena el tama�o de cada l�nea
private static final int LINESIZE_LENGTH = 1024 * 1024;
//Codificaci�n para el fichero temporal de lineas
private static final String DEFAULT_INTERNAL_ENCODING = "UTF-8";
//N�mero m�ximo de caracteres en el buffer de lectura del fichero original
private static final int CHARBUFFER_LENGTH = 8*1024;
//Tama�o en bytes del buffer intermedio (buffer de escritura a fichero temporal)
/** Para que no SE DESBORDE en la codificaci�n de los car�cteres a UTF-8*/
private static final int WRITEBUFFER_LENGTH = CHARBUFFER_LENGTH * 4;
int lineSize[];
int actualLine;
InputStreamReader inStream = null;
BufferedOutputStream outStream = null;
RandomAccessFile raf;
ByteBuffer byteBuffer;
CharBuffer charBuffer;
CharsetEncoder encoder;
CharsetDecoder decoder;
public void init(String inputFile, String charset){
charBuffer = CharBuffer.wrap(new char[8*1024]);
byteBuffer = ByteBuffer.wrap(new byte[WRITEBUFFER_LENGTH]);
try {
inStream = new InputStreamReader(new FileInputStream(inputFile), Charset.forName(charset));
outStream = new BufferedOutputStream(new FileOutputStream(inputFile+".temp"));
File file = new File(inputFile+".temp");
raf = new RandomAccessFile(file, "r");
Charset internalCharset = Charset.forName(DEFAULT_INTERNAL_ENCODING);
encoder = internalCharset.newEncoder();
decoder = internalCharset.newDecoder();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void close() {
try {
inStream.close();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read(){
char c;
int encodedBytes = 0;
int numChars;
int firstPosition;
try {
numChars = inStream.read(charBuffer);
while (numChars!=-1){
firstPosition = 0;
charBuffer.flip();
for (int i = 0; i<numChars; i++){
c = charBuffer.get(i);
if (c!='\r'){
if (c=='\n'){
createLineSize();
//Escribir lo que haya
encodedBytes = encode(firstPosition, i);
lineSize[actualLine] += encodedBytes;
//new line
actualLine++;
lineSize[actualLine]=0;
firstPosition = i+1;
}
}else{
createLineSize();
//Escribir lo que haya, como si fuera un salto de l�nea
//pero sin actualizar el n�mero de la l�nea.
encodedBytes = encode(firstPosition, i);
lineSize[actualLine] += encodedBytes;
firstPosition = i+1;
}
}
//Puede que la �ltima l�nea no termine con salto de l�nea.
if (charBuffer.hasRemaining()){
createLineSize();
//Escribir lo que haya
encodedBytes = encode(firstPosition);
lineSize[actualLine] += encodedBytes;
}
outStream.write(byteBuffer.array(),0,byteBuffer.position());
charBuffer.clear();
byteBuffer.clear();
numChars = inStream.read(charBuffer);
}
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createLineSize(){
if (lineSize==null){
lineSize = new int[LINESIZE_LENGTH];
}
}
private int encode(int offset, int limit){
int initCharBuffPos = byteBuffer.position();
int oldLimit = charBuffer.limit();
charBuffer.position(offset);
charBuffer.limit(limit);
encoder.encode(charBuffer, byteBuffer, false);
charBuffer.limit(oldLimit);
return (byteBuffer.position()-initCharBuffPos);
}
private int encode(int offset){
int initCharBuffPos = byteBuffer.position();
charBuffer.position(offset);
encoder.encode(charBuffer, byteBuffer, false);
return (byteBuffer.position()-initCharBuffPos);
}
/**
* Devuelve el n�mero de la �ltima l�nea le�da
* @return !=-1 l�nea le�da, -1 no hay l�neas le�das
*/
public int getActualLine(){
if (lineSize==null){
return -1;
}else{
return actualLine;
}
}
/**
*
* @param lineNumber
* @return
*/
public int getLineSize(int lineNumber){
return lineSize[lineNumber];
}
public char[] getString(int line){
int offset =0;
for (int i=0; i<line;i++){
offset += lineSize[i];
}
try {
raf.seek(offset);
} catch (IOException e) {
e.printStackTrace();
}
int sizeToRead = lineSize[line];
byte[] readBytes = new byte[sizeToRead];
try {
raf.read(readBytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
CharBuffer charBuffer = decoder.decode(ByteBuffer.wrap(readBytes));
return charBuffer.array();
} catch (CharacterCodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String arg[]){
Reader reader = new Reader();
reader.init("D:\\proba2.txt", "UTF-8");
reader.read();
// long time = System.nanoTime();
// for (int i=0; i<=reader.getActualLine(); i++){
// System.out.println(reader.getString(i));
// }
// time = System.nanoTime() -time;
//
// long time2 = System.nanoTime();
// for (int i=0; i<=reader.getActualLine(); i++){
// System.out.println("at com.indra.davinci.common.socket.connector.socket.PublishedThread.run(PublishedThread.java:54)");
// }
// time2 = System.nanoTime() -time2;
long time3 = System.currentTimeMillis();
int count =0;
for (int i=0; i<=reader.getActualLine(); i++){
String cadena = new String(reader.getString(i));
if (cadena.contains("08")){
count++;
}
}
time3 = System.currentTimeMillis() -time3;
// System.out.println(time);
// System.out.println(time2);
System.out.println(count +" :: "+time3);
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reader.close();
}
}