Search in sources :

Example 1 with FastByteArrayOutputStream

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);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) LazyClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) ZipInputStream(java.util.zip.ZipInputStream) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) ByteSource(com.google.common.io.ByteSource)

Example 2 with FastByteArrayOutputStream

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);
    }
}
Also used : FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) FaweOutputStream(com.fastasyncworldedit.core.internal.io.FaweOutputStream)

Example 3 with FastByteArrayOutputStream

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);
}
Also used : LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) FastSchematicWriter(com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriter) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) FastByteArraysInputStream(com.fastasyncworldedit.core.internal.io.FastByteArraysInputStream) IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream)

Example 4 with FastByteArrayOutputStream

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)));
    }
}
Also used : ClipboardWriter(com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) MultiClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) BuiltInClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) URI(java.net.URI) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) URL(java.net.URL) URIClipboardHolder(com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) RunnableVal(com.fastasyncworldedit.core.util.task.RunnableVal) DiskOptimizedClipboard(com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) ReadOnlyClipboard(com.fastasyncworldedit.core.extent.clipboard.ReadOnlyClipboard) AffineTransform(com.sk89q.worldedit.math.transform.AffineTransform) Transform(com.sk89q.worldedit.math.transform.Transform) File(java.io.File) HashSet(java.util.HashSet) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 5 with FastByteArrayOutputStream

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;
    }
}
Also used : FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream)

Aggregations

FastByteArrayOutputStream (com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream)7 MultiClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder)2 IOException (java.io.IOException)2 ZipEntry (java.util.zip.ZipEntry)2 DiskOptimizedClipboard (com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard)1 LazyClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.LazyClipboardHolder)1 ReadOnlyClipboard (com.fastasyncworldedit.core.extent.clipboard.ReadOnlyClipboard)1 URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)1 FastSchematicWriter (com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriter)1 FastByteArraysInputStream (com.fastasyncworldedit.core.internal.io.FastByteArraysInputStream)1 FaweOutputStream (com.fastasyncworldedit.core.internal.io.FaweOutputStream)1 RunnableVal (com.fastasyncworldedit.core.util.task.RunnableVal)1 ByteSource (com.google.common.io.ByteSource)1 NBTOutputStream (com.sk89q.jnbt.NBTOutputStream)1 LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)1 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)1 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)1 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)1 BuiltInClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat)1 ClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat)1