[Demonstration 1]

[Buggy Method]
void begin() {
    m_count++;
    m_lastStarted = System.nanoTime();
}

[Buggy Hunk]
m_lastStarted = System.nanoTime();

[Patch]
m_lastStarted.set(new Long(System.nanoTime()));

[Demonstration 2]

[Buggy Method]
@Override
public void run() {
    final Map<Integer, Pair<Address, CloseableByteBufferHandler>> establishedConnections = new HashMap<>();
    final boolean[] closed = new boolean[] { false };
    try {
        OutputStream sout;
        InputStream sin;
        try {
            sout = new GZIPOutputStream(socket.getOutputStream(), true);
            sin = new GZIPInputStream(socket.getInputStream());
        } catch (IOException ee) {
            try {
                socket.close();
            } catch (IOException se) {
            }
            throw ee;
        }
        final DataOutputStream out = new DataOutputStream(sout);
        DataInputStream in = new DataInputStream(sin);
        LOGGER.debug("Accepted connection from: {}", socket.getInetAddress());
        while (true) {
            LOGGER.trace("Server waiting for connection ID");
            final int connectionId = in.readInt();
            LOGGER.trace("Server received connection ID: {}", connectionId);
            int len = in.readInt();
            if (len < 0) {
                int command = -len;
                if (command == ProxyCommons.Commands.ESTABLISH_CONNECTION) {
                    final Address address = in.readBoolean() ? new Address(in.readUTF(), in.readInt()) : null;
                    ReadyFactory factory = proxyServerSide.read(in);
                    //%% Ready r = new QueueReady(queue, factory.create());
                    Ready r = factory.create();
                    r.connect(address, new ReadyConnection() {

                        @Override
                        public void failed(IOException e) {
                            LOGGER.warn("Could not connect to {}", address, e);
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(-ProxyCommons.Commands.FAIL_CONNECTION);
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                        }

                        @Override
                        public void connected(FailableCloseableByteBufferHandler write) {
                            synchronized (establishedConnections) {
                                if (closed[0]) {
                                    write.close();
                                    return;
                                }
                                establishedConnections.put(connectionId, new Pair<Address, CloseableByteBufferHandler>(address, write));
                            }
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(-ProxyCommons.Commands.ESTABLISH_CONNECTION);
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                        }

                        @Override
                        public void close() {
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(0);
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                        }

                        @Override
                        public void handle(Address address, ByteBuffer buffer) {
                            if (!buffer.hasRemaining()) {
                                return;
                            }
                            synchronized (out) {
                                try {
                                    out.writeInt(connectionId);
                                    out.writeInt(buffer.remaining());
                                    if (address == null) {
                                        out.writeBoolean(false);
                                    } else {
                                        out.writeBoolean(true);
                                        out.writeUTF(address.getHost());
                                        out.writeInt(address.getPort());
                                    }
                                    out.write(buffer.array(), buffer.arrayOffset(), buffer.remaining());
                                    out.flush();
                                } catch (IOException ioe) {
                                    try {
                                        out.close();
                                    } catch (IOException se) {
                                    }
                                }
                            }
                            buffer.position(buffer.position() + buffer.remaining());
                        }
                    });
                }
            } else if (len == 0) {
                Pair<Address, CloseableByteBufferHandler> connection;
                synchronized (establishedConnections) {
                    connection = establishedConnections.remove(connectionId);
                }
                if (connection != null) {
                    if (connection.second != null) {
                        connection.second.close();
                    }
                }
            } else {
                Pair<Address, CloseableByteBufferHandler> connection;
                synchronized (establishedConnections) {
                    connection = establishedConnections.get(connectionId);
                }
                Address a;
                if (in.readBoolean()) {
                    a = new Address(in.readUTF(), in.readInt());
                } else {
                    a = (connection == null) ? null : connection.first;
                }
                byte[] b = new byte[len];
                in.readFully(b);
                if (connection != null) {
                    if (!addressesToFilter.contains(a)) {
                        if (connection.second != null) {
                            connection.second.handle(a, ByteBuffer.wrap(b));
                        }
                    }
                } else {
                    LOGGER.debug("Invalid connection ID: {}", connectionId);
                }
            }
        }
    } catch (IOException e) {
        LOGGER.info("Socket closed by peer: {}", e.getMessage());
        try {
            socket.close();
        } catch (IOException ioe) {
        }
        List<CloseableByteBufferHandler> toClose = new LinkedList<>();
        synchronized (establishedConnections) {
            closed[0] = true;
            for (Pair<Address, CloseableByteBufferHandler> connection : establishedConnections.values()) {
                if (connection.second != null) {
                    toClose.add(connection.second);
                }
            }
        }
        for (CloseableByteBufferHandler c : toClose) {
            c.close();
        }
    }
}

[Buggy Hunk]
out.write(buffer.array(), buffer.arrayOffset(), buffer.remaining());

[Patch]
out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());

[Demonstration 3]

[Buggy Method]
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> results) throws Exception {
    if (discardingTooLongFrame) {
        bytesToDiscard = discard(buffer, bytesToDiscard);
        // If we have discarded everything, throw the exception
        if (bytesToDiscard <= 0)
            fail();
        return;
    }
    // Wait until we have read at least the short header
    if (buffer.readableBytes() < Header.LEGACY_LENGTH)
        return;
    int idx = buffer.readerIndex();
    int firstByte = buffer.getByte(idx++);
    Message.Direction direction = Message.Direction.extractFromVersion(firstByte);
    int version = firstByte & 0x7F;
    if (version > Server.CURRENT_VERSION)
        throw new ProtocolException("Invalid or unsupported protocol version: " + version);
    // Wait until we have the complete V3+ header
    if (version >= Server.VERSION_3 && buffer.readableBytes() < Header.MODERN_LENGTH)
        return;
    int flags = buffer.getByte(idx++);
    int streamId, headerLength;
    if (version >= Server.VERSION_3) {
        streamId = buffer.getShort(idx);
        idx += 2;
        headerLength = Header.MODERN_LENGTH;
    } else {
        streamId = buffer.getByte(idx);
        idx++;
        headerLength = Header.LEGACY_LENGTH;
    }
    // This throws a protocol exceptions if the opcode is unknown
    Message.Type type;
    try {
        type = Message.Type.fromOpcode(buffer.getByte(idx++), direction);
    } catch (ProtocolException e) {
        throw ErrorMessage.wrap(e, streamId);
    }
    long bodyLength = buffer.getUnsignedInt(idx);
    idx += Header.BODY_LENGTH_SIZE;
    if (bodyLength < 0) {
        buffer.skipBytes(headerLength);
        throw ErrorMessage.wrap(new ProtocolException("Invalid frame body length: " + bodyLength), streamId);
    }
    long frameLength = bodyLength + headerLength;
    if (frameLength > MAX_FRAME_LENGTH) {
        // Enter the discard mode and discard everything received so far.
        discardingTooLongFrame = true;
        tooLongStreamId = streamId;
        tooLongFrameLength = frameLength;
        bytesToDiscard = discard(buffer, frameLength);
        if (bytesToDiscard <= 0)
            fail();
        return;
    }
    if (buffer.readableBytes() < frameLength)
        return;
    // extract body
    ByteBuf body = buffer.slice(idx, (int) bodyLength);
    body.retain();
    idx += bodyLength;
    buffer.readerIndex(idx);
    Connection connection = ctx.channel().attr(Connection.attributeKey).get();
    if (connection == null) {
        // First message seen on this channel, attach the connection object
        connection = factory.newConnection(ctx.channel(), version);
        ctx.channel().attr(Connection.attributeKey).set(connection);
    } else if (connection.getVersion() != version) {
        throw ErrorMessage.wrap(new ProtocolException(String.format("Invalid message version. Got %d but previous messages on this connection had version %d", version, connection.getVersion())), streamId);
    }
    results.add(new Frame(new Header(version, flags, streamId, type), body));
}

[Buggy Hunk]
int version = firstByte & 0x7F;

[Patch]
int version = firstByte & PROTOCOL_VERSION_MASK;