I make a Circular Buffer with normal Stream.
I send to you, could be a start point for NIO.
import java.io.*;
import java.text.*;
/**
* CircularFileOutputStream -
* Fecha de creaci髇: (29/8/01 13:26:08)
* @author: JPBM
*/
public class CircularFileOutputStream extends java.io.OutputStream {
public final static int DEFAULTMAGIC = 1246773837;
// Numero Magico en HEXADECIMAL 4A50424D
public final static int DEFAULTSIZE = 65536; // 64 Kb Tama駉 por defecto
public final static String DEFAULTOPEN = "rw";
// por defecto lectura escritura
public static String LINESEPARATOR = System.getProperty("line.separator");
public static String DEFAULTEOF = LINESEPARATOR + "EOF" + LINESEPARATOR;
// Fin de fichero
public final static int SIZEOFHEADER = 12; // Tama駉 de la cabecera
public final static int OFFSETPOS = 4; // Desplazamiento de Pos actual
private SimpleDateFormat formatoFecha = new SimpleDateFormat("hh:mm:ss.SSS");
// Variable que se utiliza para crear un formato para escribir la fecha
private RandomAccessFile raf; // Fichero de acceso aleatorio
private long posactual = -1; // Posicion actual dentro del fichero
private long size = -1; // Tama駉 maximo
/**
* Constructor
* @param file java.io.File
* @exception java.io.IOException La descripci髇 de excepci髇.
*/
public CircularFileOutputStream(java.io.File file) throws java.io.FileNotFoundException, java.io.IOException {
this(file.getPath(), DEFAULTSIZE);// Tama駉 por defecto
}
/**
* Constructor
* @param file java.io.File Fichero a escribir.
* @param maxsize int Maximo tama駉 del fichero circular.
* @exception java.io.IOException La descripci髇 de excepci髇.
*/
public CircularFileOutputStream(java.io.File file, long maxsize) throws java.io.FileNotFoundException, java.io.IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(file.getPath());
}
raf = new RandomAccessFile(file, DEFAULTOPEN);
checkMagic(DEFAULTMAGIC, maxsize);// Comprueba el n鷐ero MAGICO
}
/**
* Constructor.
* @param name java.lang.String
* @exception java.io.FileNotFoundException La descripci髇 de excepci髇.
*/
public CircularFileOutputStream(String name) throws java.io.FileNotFoundException, java.io.IOException {
this(name, DEFAULTSIZE);// Se pone el tama駉 por defecto
}
/**
* Constructor.
* @param name java.lang.String
* @exception java.io.FileNotFoundException La descripci髇 de excepci髇.
*/
public CircularFileOutputStream(String name, long maxsize) throws java.io.FileNotFoundException, java.io.IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(name);
}
raf = new RandomAccessFile(name, DEFAULTOPEN);
checkMagic(DEFAULTMAGIC, maxsize);// Comprueba el n鷐ero MAGICO
}
public void close() throws IOException {
raf.close();
}
public void checkMagic(int magic, long maxsize) throws IOException {
long pos = 0;
if (raf.length() > SIZEOFHEADER + getEOFString().length()) {
raf.seek(0);
raf.writeInt(magic);
pos = getPos(); // Comprueba la posici髇
if(pos>maxsize)pos = pos % maxsize;
setSize(maxsize); // Pone el tama駉 del fichero
} else {
setSize(maxsize); // Pone el tama駉 del fichero
raf.seek(0); // Se situa en el inicio y pone el n鷐ero magico
raf.writeInt(magic);// Pone el numero magico
}
setPos(pos); // Comprueba la posici髇 Cero en caso de que no haya magic
}
/**
* Fecha de creaci髇: (31/7/00 17:11:05)
*/
public void finalize()
{
try{
raf.close();
}catch(Throwable t)
{
}
}
public void flush() throws IOException {
}
/**
* Inserte aqu?la descripci髇 del m閠odo.
* Fecha de creaci髇: (30/8/01 10:57:00)
* @return String
*/
public String getEOFString(){
return DEFAULTEOF + formatoFecha.format(new java.util.Date())+LINESEPARATOR;
}
/**
* Inserte aqu?la descripci髇 del m閠odo.
* Fecha de creaci髇: (30/8/01 10:57:00)
* @return long
*/
public long getPos()throws java.io.IOException{
if(posactual<SIZEOFHEADER)
return readPos();
else
return posactual-SIZEOFHEADER;
}
/**
* Inserte aqu?la descripci髇 del m閠odo.
* Fecha de creaci髇: (30/8/01 10:57:00)
* @return long
*/
public long getSize()throws IOException {
if(size>SIZEOFHEADER)
return size;
else return raf.length();
}
/**
* Inserte aqu?la descripci髇 del m閠odo.
* Fecha de creaci髇: (30/8/01 13:01:44)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
try{
PrintWriter cf = new PrintWriter(new CircularFileOutputStream("d:\\juanpfich"), true);
cf.println("Hola soy edui");
for(int i =0; i <= 35000; i ++)
{
cf.println("Hola soy edui" + i);
}
}catch(IOException t)
{
System.out.println(t);
t.printStackTrace();
}
}
/**
* Inserte aqu?la descripci髇 del m閠odo.
* Fecha de creaci髇: (30/8/01 10:57:00)
* @return long
*/
public long readPos() throws java.io.IOException{
raf.seek(OFFSETPOS);// Lee a partir de la cuarta posicion
return raf.readLong();// Lee a partir de la cuarta posicion
}
/**
* Inserte aqu?la descripci髇 del m閠odo.
* Fecha de creaci髇: (30/8/01 10:57:00)
* @param newPos long
*/
private synchronized void setPos(long newPos)throws IOException {
if (newPos < 0) {
throw new IllegalArgumentException("Pos < 0");
}
posactual = newPos+SIZEOFHEADER;// Pone la nueva posicion
raf.seek(OFFSETPOS);// Se situa
raf.writeLong(newPos);// Guarda la posicion
writeEOFString();// A馻de la cadena
}
/**
*
* Fecha de creaci髇: (30/8/01 10:57:00)
* @param newSize long
*/
public synchronized void setSize(long newSize) throws IOException{
if (newSize < SIZEOFHEADER + getEOFString().length()) {
throw new IllegalArgumentException("File Size < " + SIZEOFHEADER);
}
size = newSize;
raf.setLength(size);
}
public void write(byte b[]) throws IOException {
write(b,0, b.length);
}
public synchronized void write(byte b[], int off, int len) throws IOException {
intini = (int)((getSize()) -(getPos() + SIZEOFHEADER) )- len;
// Escribe en el inicio
if(ini>=0)
{
raf.seek(posactual);
raf.write(b,off,len);
setPos(posactual+ len-SIZEOFHEADER);
}else{
// Reescribe
ini = ini * (-1);
raf.seek(posactual);
raf.write(b, off, len -ini);
raf.seek(SIZEOFHEADER);
raf.write(b,off+len -ini,ini);
setPos(ini);
}
}
public synchronized void write(int b) throws java.io.IOException
{
long pos = posactual % size;
if(posactual >= size)
{
raf.seek(SIZEOFHEADER);// Posicion actual
raf.write(b);
setPos(0);// Pone la posicion inicial
}else{
raf.seek(pos);// Posicion actual
raf.write(b);
setPos((pos+1)-SIZEOFHEADER);// Pone una posactual mas (para eso resto 8)
}
}
/**
* Inserte aqu?la descripci髇 del m閠odo.
* Fecha de creaci髇: (30/8/01 10:57:00)
* @param newPos long
*/
private void writeEOFString()throws IOException {
String cadena = getEOFString();// A馻de la cadena
intini = (int)((getSize()) - (getPos()+SIZEOFHEADER)) - cadena.length();
// Escribe en el inicio
if(ini>=0)
{
raf.seek(posactual);
raf.writeBytes(cadena);
}else{
// Reescribe
ini = ini * -1;
raf.seek(posactual);
raf.writeBytes(cadena.substring(0, cadena.length()-ini));
raf.seek(SIZEOFHEADER);
raf.writeBytes(cadena.substring(cadena.length()-ini, cadena.length()) );
}
}
}