Spaces:
Build error
Build error
File size: 3,118 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
package net.minecraft.util.eventlog;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import java.io.Closeable;
import java.io.IOException;
import java.io.Writer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
public class JsonEventLog<T> implements Closeable {
private static final Gson GSON = new Gson();
private final Codec<T> codec;
final FileChannel channel;
private final AtomicInteger referenceCount = new AtomicInteger(1);
public JsonEventLog(Codec<T> p_261608_, FileChannel p_262072_) {
this.codec = p_261608_;
this.channel = p_262072_;
}
public static <T> JsonEventLog<T> open(Codec<T> p_261795_, Path p_261489_) throws IOException {
FileChannel filechannel = FileChannel.open(p_261489_, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
return new JsonEventLog<>(p_261795_, filechannel);
}
public void write(T p_261929_) throws IOException {
JsonElement jsonelement = this.codec.encodeStart(JsonOps.INSTANCE, p_261929_).getOrThrow(IOException::new);
this.channel.position(this.channel.size());
Writer writer = Channels.newWriter(this.channel, StandardCharsets.UTF_8);
GSON.toJson(jsonelement, 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> jsoneventlogreader = JsonEventLogReader.create(
this.codec, Channels.newReader(this.channel, StandardCharsets.UTF_8)
);
return new JsonEventLogReader<T>() {
private volatile long position;
@Nullable
@Override
public T next() throws IOException {
Object object;
try {
JsonEventLog.this.channel.position(this.position);
object = jsoneventlogreader.next();
} finally {
this.position = JsonEventLog.this.channel.position();
}
return (T)object;
}
@Override
public void close() throws IOException {
JsonEventLog.this.releaseReference();
}
};
}
}
@Override
public void close() throws IOException {
this.releaseReference();
}
void releaseReference() throws IOException {
if (this.referenceCount.decrementAndGet() <= 0) {
this.channel.close();
}
}
} |