Example usage for io.netty.util ReferenceCountUtil release

List of usage examples for io.netty.util ReferenceCountUtil release

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil release.

Prototype

public static boolean release(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

From source file:uk.ac.lancs.stopcock.proxy.ProxiedConnection.java

License:Apache License

/**
 * Construct a suitable OpenFlow echo request based upon the OpenFlow version the upstream channel advertised
 * version.//w w w.j  a  va2  s  .c  om
 *
 * @return Container with a suitable echo request
 */
public Container createPing() {
    ByteBuf byteBuf = upstream.alloc().buffer(8);
    NettyCompatibilityChannelBuffer compatBuffer = new NettyCompatibilityChannelBuffer(byteBuf);

    OFEchoRequest request = OFFactories.getFactory(upstreamVersion).echoRequest(ECHO_DATA);
    request.writeTo(compatBuffer);

    byte[] rawData = new byte[compatBuffer.readableBytes()];

    compatBuffer.resetReaderIndex();
    compatBuffer.readBytes(rawData);

    ReferenceCountUtil.release(byteBuf);

    Header header = new Header((short) upstreamVersion.getWireVersion(), (short) Type.OFPT_ECHO_REQUEST.getId(),
            8, request.getXid());
    return new Container(header, rawData, Type.OFPT_ECHO_REQUEST, request);
}

From source file:uk.co.thinkofdeath.thinkcraft.bukkit.world.ChunkManager.java

License:Apache License

/**
 * Marks the as inactive so the map viewer will use a
 * cached version of the chunk/*  w  w w  .  j  av a 2s . c  o m*/
 *
 * @param chunk
 *         The chunk to mark as inactive
 */
public void deactivateChunk(Chunk chunk) {
    // Stop the map viewer from requesting live versions
    synchronized (activeChunks) {
        activeChunks.remove(chunkKey(chunk.getX(), chunk.getZ()));
    }
    // Grab a final copy to save to the region file
    final ChunkSnapshot snapshot = chunk.getChunkSnapshot(false, true, false);
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

        @Override
        public void run() {
            try {
                // Lock the world for writing
                Lock lock = worldLock.writeLock();
                lock.lock();

                File worldFolder = new File(plugin.getWorldDir(), world.getName());
                if (!worldFolder.exists() && !worldFolder.mkdirs()) {
                    throw new RuntimeException("Failed to create world folder");
                }

                ByteBuf data = allocator.buffer();

                try (RandomAccessFile region = new RandomAccessFile(
                        new File(worldFolder,
                                String.format("region_%d-%d.dat", snapshot.getX() >> 5, snapshot.getZ() >> 5)),
                        "rw")) {
                    // Save and compress the chunk
                    gzipChunk(snapshot, data);

                    if (region.length() < 4096 * 3) {
                        // Init header with enough space for size + location
                        // with a little bit extra for expansion
                        region.seek(4096 * 3);
                        region.writeByte(0);
                    }
                    int id = ((snapshot.getX() & 0x1F) | ((snapshot.getZ() & 0x1F) << 5));
                    region.seek(8 * id);
                    int offset = region.readInt();
                    int size = region.readInt();
                    if (offset != 0) {
                        // Try and reuse the old space
                        if (data.readableBytes() < ((size / 4096) + 1) * 4096) {
                            size = data.readableBytes();
                            region.seek(8 * id);
                            region.writeInt(offset);
                            region.writeInt(size);
                            region.seek(offset * 4096);
                            byte[] bytes = new byte[data.readableBytes()];
                            data.readBytes(bytes);
                            region.write(bytes);
                            return;
                        }
                    }

                    // Search for a new location

                    // Fill in the used spaces first
                    boolean[] usedSpace = new boolean[(int) ((region.length() / 4096) + 1)];
                    usedSpace[0] = usedSpace[1] = usedSpace[2] = true;
                    for (int i = 0; i < 32 * 32; i++) {
                        if (i == id)
                            continue;
                        region.seek(8 * i);
                        int oo = region.readInt();
                        int os = region.readInt();
                        for (int j = oo; j < oo + ((os / 4096) + 1); j++) {
                            usedSpace[j] = true;
                        }
                    }
                    offset = usedSpace.length;
                    size = data.readableBytes();
                    // Search though every location until a location with a large enough
                    // space is found
                    search: for (int i = 2; i < usedSpace.length; i++) {
                        if (!usedSpace[i]) {
                            for (int j = i + 1; j < i + ((size / 4096) + 1); j++) {
                                if (j >= usedSpace.length || usedSpace[j]) {
                                    i += ((size / 4096) + 1);
                                    continue search;
                                }
                            }
                            offset = i;
                            break;
                        }
                    }
                    region.seek(offset * 4096);
                    byte[] bytes = new byte[data.readableBytes()];
                    data.readBytes(bytes);
                    region.write(bytes);
                    region.seek(8 * id);
                    region.writeInt(offset);
                    region.writeInt(size);
                } finally {
                    lock.unlock();
                    ReferenceCountUtil.release(data);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}