public byte[] getNextTag() {
        byte[] tagBytes = null;

        if (tagPool != null) {
            tagBytes = tagPool.pollFirst();
        }

        if (tagBytes == null) {
            long tag = nextTagId++;
            int size = encodingSize(tag);

            tagBytes = new byte[size];

            for (int i = 0; i < size; ++i) {
                tagBytes[size - 1 - i] = (byte) (tag >>> (i * 8));
            }
        }

        return tagBytes;
    }
@Test
    public void testTagGenerationWorksWithIdRollover() throws Exception {
        AmqpTransferTagGenerator tagGen = new AmqpTransferTagGenerator(false);

        Field urisField = tagGen.getClass().getDeclaredField("nextTagId");
        urisField.setAccessible(true);
        urisField.set(tagGen, Long.MAX_VALUE + 1);

        {
            byte[] tag = tagGen.getNextTag();

            ByteArrayInputStream bais = new ByteArrayInputStream(tag);
            DataInputStream dis = new DataInputStream(bais);

            assertEquals(8, tag.length);
            assertEquals(Long.MAX_VALUE + 1, dis.readLong());
        }
        {
            byte[] tag = tagGen.getNextTag();

            ByteArrayInputStream bais = new ByteArrayInputStream(tag);
            DataInputStream dis = new DataInputStream(bais);

            assertEquals(8, tag.length);
            assertEquals(Long.MAX_VALUE + 2, dis.readLong());
        }
    }