RealmsUploadWorldPacker .java

com.mojang.realmsclient.client.worldupload.RealmsUploadWorldPacker (class): members: WORLD_FOLDER_NAME, directoryToPack, SIZE_LIMIT, isCanceled, pack.

com.mojang.realmsclient.client.worlduploadclass87 linesclient2.7 KB

API Surface (1)

File pack(final Path directoryToPack, final BooleanSupplier isCanceled)

Fields & Constants (4)

SIZE_LIMIT
WORLD_FOLDER_NAME
isCanceled
directoryToPack

String literals (3)

"world"
"realms-upload-file"
".tar.gz"

Code Graph: Dependencies (2)

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

Full Source

RealmsUploadWorldPacker.java87 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.client.worldupload;
2
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.nio.file.Path;
9
import java.util.function.BooleanSupplier;
10
import java.util.zip.GZIPOutputStream;
11
import net.fabricmc.api.EnvType;
12
import net.fabricmc.api.Environment;
13
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
14
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
15
16
@Environment(EnvType.CLIENT)
17
public class RealmsUploadWorldPacker {
18
	private static final long SIZE_LIMIT = 5368709120L;
19
	private static final String WORLD_FOLDER_NAME = "world";
20
	private final BooleanSupplier isCanceled;
21
	private final Path directoryToPack;
22
23
	public static File pack(final Path directoryToPack, final BooleanSupplier isCanceled) throws IOException {
24
		return new RealmsUploadWorldPacker(directoryToPack, isCanceled).tarGzipArchive();
25
	}
26
27
	private RealmsUploadWorldPacker(final Path directoryToPack, final BooleanSupplier isCanceled) {
28
		this.isCanceled = isCanceled;
29
		this.directoryToPack = directoryToPack;
30
	}
31
32
	private File tarGzipArchive() throws IOException {
33
		TarArchiveOutputStream tar = null;
34
35
		try {
36
			File file = File.createTempFile("realms-upload-file", ".tar.gz");
37
			tar = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
38
			tar.setLongFileMode(3);
39
			this.addFileToTarGz(tar, this.directoryToPack, "world", true);
40
			if (this.isCanceled.getAsBoolean()) {
41
				throw new RealmsUploadCanceledException();
42
			}
43
44
			tar.finish();
45
			this.verifyBelowSizeLimit(file.length());
46
			return file;
47
		} finally {
48
			if (tar != null) {
49
				tar.close();
50
			}
51
		}
52
	}
53
54
	private void addFileToTarGz(final TarArchiveOutputStream out, final Path path, final String base, final boolean root) throws IOException {
55
		if (this.isCanceled.getAsBoolean()) {
56
			throw new RealmsUploadCanceledException();
57
		}
58
59
		this.verifyBelowSizeLimit(out.getBytesWritten());
60
		File file = path.toFile();
61
		String entryName = root ? base : base + file.getName();
62
		TarArchiveEntry entry = new TarArchiveEntry(file, entryName);
63
		out.putArchiveEntry(entry);
64
		if (file.isFile()) {
65
			try (InputStream is = new FileInputStream(file)) {
66
				is.transferTo(out);
67
			}
68
69
			out.closeArchiveEntry();
70
		} else {
71
			out.closeArchiveEntry();
72
			File[] children = file.listFiles();
73
			if (children != null) {
74
				for (File child : children) {
75
					this.addFileToTarGz(out, child.toPath(), entryName + "/", false);
76
				}
77
			}
78
		}
79
	}
80
81
	private void verifyBelowSizeLimit(final long sizeInByte) {
82
		if (sizeInByte > 5368709120L) {
83
			throw new RealmsUploadTooLargeException(5368709120L);
84
		}
85
	}
86
}
87

Decompiled Minecraft 26.3 source of RealmsUploadWorldPacker (com/mojang/realmsclient/client/worldupload/RealmsUploadWorldPacker.java) with Mojang official mappings, stored verbatim on the site.