Skip to content

Instantly share code, notes, and snippets.

@faloi
Created June 7, 2014 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faloi/9535c21d8cd88df6c124 to your computer and use it in GitHub Desktop.
Save faloi/9535c21d8cd88df6c124 to your computer and use it in GitHub Desktop.
Posible solucion (super incompleta) del problema del File System. Clase del viernes 7/6/2014
public class Buffer {
int getTamanio()
byte[] getContenido()
}
public class File {
LowLevelFileSystem fileSystem;
int fileDescriptor;
public File(String path, LowLevelFileSystem fileSystem) {
}
void abrir() {
this.fileDescriptor = fileSystem.open(this.path);
}
Buffer leerSincronico(Buffer buffer) {
validarEstaAbierto();
byte[] buffer = crearBuffer(this.size);
fileSystem.syncReadFile(
this.fileDescriptor,
buffer,
comienzo, end);
return buffer;
}
void escribir(int comienzo, int end, byte[] contenido) {
validarTodo(comienzo, end, contenido);
fileSystem.syncWriteFile(
this.fileDescriptor,
buffer,
comienzo,
end
);
}
void leerAsync(Buffer buffer, Consumer<Buffer> onSuccess,
Consumer<void> onFailure) {
asyncReadFile(fileDescriptor,
buffer.getContenido(),
buffer.getInicio(),
buffer.getFin(),
cantBytesLeidos -> {
if (cantBytesLeidos < buffer.getTamanio()) {
onFailure()
} else {
buffer.setFin(cantBytesLeidos);
onSuccess(buffer);
}
}
);
}
}
File file = new File("/usr/fede/algo.txt", lowLevelFileSystem)
.abrir()
.leerAsync(
new Buffer(0, 20),
buffer -> mostrarPorPantalla(buffer),
() -> mostrarPorPantalla("ups")
);
//test1
file.leerSincronico(0, 20);
//test2
file.leerSincronico(20, 30);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment