public static List<ClientMessage> getFragments(int maxFrameSize, ClientMessage clientMessage) {
        if (clientMessage.getFrameLength() <= maxFrameSize) {
            return Collections.singletonList(clientMessage);
        }
        long fragmentId = FRAGMENT_ID_SEQUENCE.next();
        LinkedList<ClientMessage> fragments = new LinkedList<>();
        ClientMessage.ForwardFrameIterator iterator = clientMessage.frameIterator();

        ReadState state = ReadState.BEGINNING;
        int length = 0;
        ClientMessage fragment = null;
        while (iterator.hasNext()) {
            ClientMessage.Frame frame = iterator.peekNext();
            int frameSize = frame.getSize();
            length += frameSize;

            if (frameSize > maxFrameSize) {
                iterator.next();
                if (state == ReadState.MIDDLE) {
                    fragments.add(fragment);
                }
                fragment = createFragment(fragmentId);
                fragment.add(frame.copy());
                fragments.add(fragment);
                state = ReadState.BEGINNING;
                length = 0;
            } else if (length <= maxFrameSize) {
                iterator.next();
                if (state == ReadState.BEGINNING) {
                    fragment = createFragment(fragmentId);
                }
                fragment.add(frame.copy());
                state = ReadState.MIDDLE;
            } else {
                assert state == ReadState.MIDDLE;
                fragments.add(fragment);
                state = ReadState.BEGINNING;
                length = 0;
            }
        }
        if (state == ReadState.MIDDLE) {
            fragments.add(fragment);
        }
        fragments.getFirst().getStartFrame().flags |= BEGIN_FRAGMENT_FLAG;
        fragments.getLast().getStartFrame().flags |= END_FRAGMENT_FLAG;
        return fragments;
    }
@Test
    public void testGetSubFrames() {
        List<ClientMessage> fragments = getFragments(128, clientMessage);
        ClientMessage.ForwardFrameIterator originalIterator = clientMessage.frameIterator();
        assertEquals(19, fragments.size());

        assertFragments(fragments, originalIterator);
    }