use of com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream in project FastAsyncWorldEdit by IntellectualSites.
the class ClipboardFormats method loadAllFromUrl.
public static MultiClipboardHolder loadAllFromUrl(URL url) throws IOException {
List<LazyClipboardHolder> clipboards = new ArrayList<>();
try (ReadableByteChannel rbc = Channels.newChannel(url.openStream())) {
try (InputStream in = Channels.newInputStream(rbc)) {
try (ZipInputStream zip = new ZipInputStream(in)) {
ZipEntry entry;
byte[] buffer = new byte[8192];
while ((entry = zip.getNextEntry()) != null) {
String filename = entry.getName();
ClipboardFormat format = findByExtension(filename);
if (format != null) {
FastByteArrayOutputStream out = new FastByteArrayOutputStream();
int len;
while ((len = zip.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
byte[] array = out.toByteArray();
ByteSource source = ByteSource.wrap(array);
LazyClipboardHolder clipboard = new LazyClipboardHolder(url.toURI(), source, format, null);
clipboards.add(clipboard);
}
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
LazyClipboardHolder[] arr = clipboards.toArray(new LazyClipboardHolder[0]);
try {
MultiClipboardHolder multi = new MultiClipboardHolder(url.toURI());
for (LazyClipboardHolder h : arr) {
multi.add(h);
}
return multi;
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
use of com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream in project FastAsyncWorldEdit by IntellectualSites.
the class ChunkPacket method apply.
@Override
public byte[] apply(byte[] buffer) {
try {
byte[] sectionBytes = getSectionBytes();
FastByteArrayOutputStream baos = new FastByteArrayOutputStream(buffer);
FaweOutputStream fos = new FaweOutputStream(baos);
fos.writeInt(this.chunkX);
fos.writeInt(this.chunkZ);
fos.writeBoolean(this.full);
fos.writeVarInt(getChunk().getBitMask());
fos.writeNBT("", getHeightMap());
fos.writeVarInt(sectionBytes.length);
fos.write(sectionBytes);
// TODO entities / NBT
// Set<CompoundTag> entities = chunk.getEntities();
// Map<BlockVector3, CompoundTag> tiles = chunk.getTiles();
// (Entities / NBT)
fos.writeVarInt(0);
return baos.toByteArray();
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
use of com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream in project FastAsyncWorldEdit by IntellectualSites.
the class CompressedSchematicTag method adapt.
@Override
public LZ4BlockInputStream adapt(Clipboard src) throws IOException {
FastByteArrayOutputStream blocksOut = new FastByteArrayOutputStream();
try (LZ4BlockOutputStream lz4out = new LZ4BlockOutputStream(blocksOut)) {
NBTOutputStream nbtOut = new NBTOutputStream(lz4out);
new FastSchematicWriter(nbtOut).write(getSource());
} catch (IOException e) {
throw new RuntimeException(e);
}
FastByteArraysInputStream in = new FastByteArraysInputStream(blocksOut.toByteArrays());
return new LZ4BlockInputStream(in);
}
use of com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream in project FastAsyncWorldEdit by IntellectualSites.
the class ClipboardCommands method download.
// FAWE start
@Command(name = "download", aliases = { "/download" }, desc = "Downloads your clipboard through the configured web interface")
@Deprecated
@CommandPermissions({ "worldedit.clipboard.download" })
public void download(final Actor actor, final LocalSession session, @Arg(name = "format", desc = "String", def = "fast") final String formatName) throws WorldEditException {
final ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
if (format == null) {
actor.print(Caption.of("fawe.worldedit.clipboard.clipboard.invalid.format", formatName));
return;
}
actor.print(Caption.of("fawe.web.generating.link", formatName));
ClipboardHolder holder = session.getClipboard();
URL url;
if (holder instanceof MultiClipboardHolder) {
MultiClipboardHolder multi = (MultiClipboardHolder) holder;
Set<File> files = new HashSet<>();
Set<URI> invalid = new HashSet<>();
for (ClipboardHolder cur : multi.getHolders()) {
if (cur instanceof URIClipboardHolder) {
URIClipboardHolder uriHolder = (URIClipboardHolder) cur;
URI uri = uriHolder.getUri();
File file = new File(uri.getPath());
if (file.exists() && file.isFile()) {
files.add(file.getAbsoluteFile());
} else if (!uri.getPath().isEmpty()) {
invalid.add(uri);
}
}
}
final LocalConfiguration config = WorldEdit.getInstance().getConfiguration();
final File working = WorldEdit.getInstance().getWorkingDirectoryFile(config.saveDir).getAbsoluteFile();
url = MainUtil.upload(null, null, "zip", new RunnableVal<>() {
@Override
public void run(OutputStream out) {
try (ZipOutputStream zos = new ZipOutputStream(out)) {
for (File file : files) {
String fileName = file.getName();
if (MainUtil.isInSubDirectory(working, file)) {
fileName = working.toURI().relativize(file.toURI()).getPath();
}
ZipEntry ze = new ZipEntry(fileName);
zos.putNextEntry(ze);
Files.copy(file.toPath(), zos);
zos.closeEntry();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
} else {
Clipboard clipboard = holder.getClipboard();
final Transform transform = holder.getTransform();
final Clipboard target;
// If we have a transform, bake it into the copy
if (!transform.isIdentity()) {
final FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform);
target = new BlockArrayClipboard(result.getTransformedRegion(), actor.getUniqueId());
target.setOrigin(clipboard.getOrigin());
Operations.completeLegacy(result.copyTo(target));
} else {
target = clipboard;
}
if (format == BuiltInClipboardFormat.PNG) {
try {
FastByteArrayOutputStream baos = new FastByteArrayOutputStream(Short.MAX_VALUE);
ClipboardWriter writer = format.getWriter(baos);
writer.write(target);
baos.flush();
url = ImgurUtility.uploadImage(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
url = null;
}
} else {
if (Settings.settings().WEB.URL.isEmpty()) {
actor.print(Caption.of("fawe.error.setting.disable", "web.url"));
return;
}
url = FaweAPI.upload(target, format);
}
}
if (url == null) {
actor.print(Caption.of("fawe.web.generating.link.failed"));
} else {
String urlText = url.toString();
actor.print(Caption.of("fawe.web.download.link", urlText).clickEvent(ClickEvent.openUrl(urlText)));
}
}
use of com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream in project FastAsyncWorldEdit by IntellectualSites.
the class MemoryOptimizedHistory method getBlockOS.
@Override
public FaweOutputStream getBlockOS(int x, int y, int z) throws IOException {
if (idsStreamZip != null) {
return idsStreamZip;
}
synchronized (this) {
setOrigin(x, z);
idsStream = new FastByteArrayOutputStream(Settings.settings().HISTORY.BUFFER_SIZE);
idsStreamZip = getCompressedOS(idsStream);
writeHeader(idsStreamZip, x, y, z);
return idsStreamZip;
}
}
Aggregations