@Override
    public void close() throws IOException {
        if(close.get()) {
            log.warn(String.format("Skip double close of stream %s", this));
            return;
        }
        try {
            if(buffer.size() > 0) {
                proxy.write(buffer.toByteArray());
            }
            // Re-use buffer
            buffer.reset();
            super.close();
        }
        finally {
            close.set(true);
        }
    }
@Test
    public void testCopy2() throws Exception {
        final ByteArrayOutputStream proxy = new ByteArrayOutputStream(40500);
        final MemorySegementingOutputStream out = new MemorySegementingOutputStream(proxy, 32768);
        final byte[] content = RandomUtils.nextBytes(40500);
        out.write(content, 0, 32768);
        out.write(content, 32768, 7732);
        out.close();
        assertArrayEquals(content, proxy.toByteArray());
    }