JsonEventLog.java
net.minecraft.util.eventlog.JsonEventLog
信息
- 全限定名:net.minecraft.util.eventlog.JsonEventLog
- 类型:public class
- 包:net.minecraft.util.eventlog
- 源码路径:src/main/java/net/minecraft/util/eventlog/JsonEventLog.java
- 起始行号:L19
- 实现:Closeable
- 职责:
TODO
字段/常量
-
GSON- 类型:
Gson - 修饰符:
private static final - 源码定位:
L20 - 说明:
TODO
- 类型:
-
codec- 类型:
Codec<T> - 修饰符:
private final - 源码定位:
L21 - 说明:
TODO
- 类型:
-
channel- 类型:
FileChannel - 修饰符:
private final - 源码定位:
L22 - 说明:
TODO
- 类型:
-
referenceCount- 类型:
AtomicInteger - 修饰符:
private final - 源码定位:
L23 - 说明:
TODO
- 类型:
内部类/嵌套类型
- 无
构造器
public JsonEventLog(Codec<T> codec, FileChannel channel) @ L25
- 构造器名:JsonEventLog
- 源码定位:L25
- 修饰符:public
参数:
- codec: Codec
- channel: FileChannel
说明:
TODO
方法
下面的方法块按源码顺序生成。
public static <T> JsonEventLog<T> open(Codec<T> codec, Path path) @ L30
- 方法名:open
- 源码定位:L30
- 返回类型:
JsonEventLog - 修饰符:public static
参数:
- codec: Codec
- path: Path
说明:
TODO
public void write(T event) @ L35
- 方法名:write
- 源码定位:L35
- 返回类型:void
- 修饰符:public
参数:
- event: T
说明:
TODO
public JsonEventLogReader<T> openReader() @ L44
- 方法名:openReader
- 源码定位:L44
- 返回类型:JsonEventLogReader
- 修饰符:public
参数:
- 无
说明:
TODO
public void close() @ L78
- 方法名:close
- 源码定位:L78
- 返回类型:void
- 修饰符:public
参数:
- 无
说明:
TODO
private void releaseReference() @ L83
- 方法名:releaseReference
- 源码定位:L83
- 返回类型:void
- 修饰符:private
参数:
- 无
说明:
TODO
代码
public class JsonEventLog<T> implements Closeable {
private static final Gson GSON = new Gson();
private final Codec<T> codec;
private final FileChannel channel;
private final AtomicInteger referenceCount = new AtomicInteger(1);
public JsonEventLog(Codec<T> codec, FileChannel channel) {
this.codec = codec;
this.channel = channel;
}
public static <T> JsonEventLog<T> open(Codec<T> codec, Path path) throws IOException {
FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
return new JsonEventLog<>(codec, channel);
}
public void write(T event) throws IOException {
JsonElement json = this.codec.encodeStart(JsonOps.INSTANCE, event).getOrThrow(IOException::new);
this.channel.position(this.channel.size());
Writer writer = Channels.newWriter(this.channel, StandardCharsets.UTF_8);
GSON.toJson(json, GSON.newJsonWriter(writer));
writer.write(10);
writer.flush();
}
public JsonEventLogReader<T> openReader() throws IOException {
if (this.referenceCount.get() <= 0) {
throw new IOException("Event log has already been closed");
} else {
this.referenceCount.incrementAndGet();
final JsonEventLogReader<T> reader = JsonEventLogReader.create(this.codec, Channels.newReader(this.channel, StandardCharsets.UTF_8));
return new JsonEventLogReader<T>() {
private volatile long position;
{
Objects.requireNonNull(JsonEventLog.this);
}
@Override
public @Nullable T next() throws IOException {
Object var1;
try {
JsonEventLog.this.channel.position(this.position);
var1 = reader.next();
} finally {
this.position = JsonEventLog.this.channel.position();
}
return (T)var1;
}
@Override
public void close() throws IOException {
JsonEventLog.this.releaseReference();
}
};
}
}
@Override
public void close() throws IOException {
this.releaseReference();
}
private void releaseReference() throws IOException {
if (this.referenceCount.decrementAndGet() <= 0) {
this.channel.close();
}
}
}引用的其他类
- JsonEventLogReader
- 引用位置:
方法调用/返回值 - 关联成员:
JsonEventLogReader.create()
- 引用位置: