JsonEventLog .java

net.minecraft.util.eventlog.JsonEventLog (class): members: openReader, referenceCount, channel, open, GSON, write, close, codec. imports: Gson, JsonOps.

net.minecraft.util.eventlogclass81 linescommon2.5 KB

Implements

API Surface (5)

<T> JsonEventLog<T> open(final Codec<T> codec, final Path path)
void write(final T event)
JsonEventLogReader<T> openReader()
void close()
void close()

Fields & Constants (4)

GSON
codec
channel
referenceCount

String literals (1)

"Event log has already been closed"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

JsonEventLog.java81 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.eventlog;
2
3
import com.google.gson.Gson;
4
import com.google.gson.JsonElement;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.JsonOps;
7
import java.io.Closeable;
8
import java.io.IOException;
9
import java.io.Writer;
10
import java.nio.channels.Channels;
11
import java.nio.channels.FileChannel;
12
import java.nio.charset.StandardCharsets;
13
import java.nio.file.Path;
14
import java.nio.file.StandardOpenOption;
15
import java.util.concurrent.atomic.AtomicInteger;
16
import org.jspecify.annotations.Nullable;
17
18
public class JsonEventLog<T> implements Closeable {
19
	private static final Gson GSON = new Gson();
20
	private final Codec<T> codec;
21
	private final FileChannel channel;
22
	private final AtomicInteger referenceCount = new AtomicInteger(1);
23
24
	public JsonEventLog(final Codec<T> codec, final FileChannel channel) {
25
		this.codec = codec;
26
		this.channel = channel;
27
	}
28
29
	public static <T> JsonEventLog<T> open(final Codec<T> codec, final Path path) throws IOException {
30
		FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
31
		return new JsonEventLog<>(codec, channel);
32
	}
33
34
	public void write(final T event) throws IOException {
35
		JsonElement json = this.codec.encodeStart(JsonOps.INSTANCE, event).getOrThrow(IOException::new);
36
		this.channel.position(this.channel.size());
37
		Writer writer = Channels.newWriter(this.channel, StandardCharsets.UTF_8);
38
		GSON.toJson(json, GSON.newJsonWriter(writer));
39
		writer.write(10);
40
		writer.flush();
41
	}
42
43
	public JsonEventLogReader<T> openReader() throws IOException {
44
		if (this.referenceCount.get() <= 0) {
45
			throw new IOException("Event log has already been closed");
46
		}
47
48
		this.referenceCount.incrementAndGet();
49
		final JsonEventLogReader<T> reader = JsonEventLogReader.create(this.codec, Channels.newReader(this.channel, StandardCharsets.UTF_8));
50
		return new JsonEventLogReader<T>() {
51
			private volatile long position;
52
53
			@Override
54
			public @Nullable T next() throws IOException {
55
				try {
56
					JsonEventLog.this.channel.position(this.position);
57
					return reader.next();
58
				} finally {
59
					this.position = JsonEventLog.this.channel.position();
60
				}
61
			}
62
63
			@Override
64
			public void close() throws IOException {
65
				JsonEventLog.this.releaseReference();
66
			}
67
		};
68
	}
69
70
	@Override
71
	public void close() throws IOException {
72
		this.releaseReference();
73
	}
74
75
	private void releaseReference() throws IOException {
76
		if (this.referenceCount.decrementAndGet() <= 0) {
77
			this.channel.close();
78
		}
79
	}
80
}
81

Decompiled Minecraft 26.3 source of JsonEventLog (net/minecraft/util/eventlog/JsonEventLog.java) with Mojang official mappings, stored verbatim on the site.