public byte[] serialize(int size) throws IOException {
        ByteArrayOutputStream ba = new ByteArrayOutputStream(size);
        DataOutputStream index = new DataOutputStream(ba);
        index.writeShort(size);
        index.writeShort(MAGIC);
        index.writeInt(nodeWeight);
        if (subNodes != null) {
            if (subNodes.size() > (int)Short.MAX_VALUE) {
                throw new IndexOutOfBoundsException();
            }
            index.writeShort(subNodes.size());
            Character[] array = new Character[subNodes.size()];
            subNodes.keySet().toArray(array);
            Arrays.sort(array);
            for (Character ch : array) {
                index.writeChar(ch.charValue());
                long childPos = subNodes.get(ch).persist();
                if (childPos <= 0) {
                    throw new IOException("try to write invalid zero position");
                }
                PointerUtils.writePointer(index, childPos);
            }
        }
        else {
            index.writeShort(0);
        }
        if (queue != null) {
            if (queue.size() > (int)Byte.MAX_VALUE) {
                throw new IndexOutOfBoundsException();
            }        
            index.write(queue.size());
            queue.serialize(index);
        }
        else {
            index.write(0);
        }
        if (originalNames != null) {
            if (originalNames.size() > (int)Short.MAX_VALUE) {
                originalNames.shrinkOriginalNames((int)Short.MAX_VALUE);
            } 
            index.writeShort(originalNames.size());
            originalNames.serialize(index);
        }
        else {
            index.writeShort(0);
        }
        ba.close();
        return ba.toByteArray();
    }