Created
June 7, 2014 06:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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