com.jtransc.io.async.impl.JTranscAsyncFileSystemNIO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jtransc-rt-core Show documentation
Show all versions of jtransc-rt-core Show documentation
JVM AOT compiler currently generating JavaScript, C++, Haxe, with initial focus on Kotlin and games.
package com.jtransc.io.async.impl;
import com.jtransc.async.JTranscAsyncHandler;
import com.jtransc.async.JTranscAsyncTools;
import com.jtransc.io.JTranscFileMode;
import com.jtransc.io.async.JTranscAsyncFileSystem;
import com.jtransc.io.async.JTranscAsyncStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class JTranscAsyncFileSystemNIO extends JTranscAsyncFileSystem {
@Override
public void getLength(final String path, final JTranscAsyncHandler handler) {
JTranscAsyncTools.runInThread(handler, new JTranscAsyncTools.ThreadRun() {
@Override
public Long run() {
return new File(path).length();
}
});
}
@Override
public void mkdir(final String path, final JTranscAsyncHandler handler) {
JTranscAsyncTools.runInThread(handler, new JTranscAsyncTools.ThreadRun() {
@Override
public Boolean run() {
new File(path).mkdir();
return true;
}
});
}
@Override
public void open(final String path, final JTranscFileMode mode, final JTranscAsyncHandler handler) {
StandardOpenOption[] options = new StandardOpenOption[0];
switch (mode) {
case READ:
options = new StandardOpenOption[]{StandardOpenOption.READ};
break;
case WRITE:
options = new StandardOpenOption[]{StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE};
break;
case APPEND:
options = new StandardOpenOption[]{StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.APPEND};
break;
}
final AsynchronousFileChannel channel;
try {
channel = AsynchronousFileChannel.open(Paths.get(path), options);
} catch (Throwable t) {
handler.complete(null, t);
return;
}
handler.complete(new JTranscAsyncStream() {
@Override
public void writeAsync(long position, byte[] data, int offset, int size, JTranscAsyncHandler callback) {
channel.write(ByteBuffer.wrap(data, offset, size), position, callback, new CompletionHandler() {
@Override
public void completed(Integer result, Object attachment) {
((JTranscAsyncHandler)attachment).complete(result, null);
}
@Override
public void failed(Throwable exc, Object attachment) {
((JTranscAsyncHandler)attachment).complete(null, exc);
}
});
}
@Override
public void readAsync(long position, byte[] data, int offset, int size, JTranscAsyncHandler callback) {
channel.read(ByteBuffer.wrap(data, offset, size), position, callback, new CompletionHandler() {
@Override
public void completed(Integer result, Object attachment) {
((JTranscAsyncHandler)attachment).complete(result, null);
}
@Override
public void failed(Throwable exc, Object attachment) {
((JTranscAsyncHandler)attachment).complete(null, exc);
}
});
}
@Override
public void setLengthAsync(long length, JTranscAsyncHandler callback) {
try {
channel.truncate(length);
} catch (IOException e) {
callback.complete(null, e);
return;
}
callback.complete(length, null);
}
@Override
public void getLengthAsync(JTranscAsyncHandler callback) {
final long size;
try {
size = channel.size();
} catch (IOException e) {
callback.complete(null, e);
return;
}
callback.complete(size, null);
}
@Override
public void closeAsync(JTranscAsyncHandler callback) {
try {
channel.close();
} catch (IOException e) {
callback.complete(null, e);
return;
}
callback.complete(0, null);
}
}, null);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy