void writePartsToStreams(
            InputStream streamIn,
            OutputStreamGetter outputStreamGetter,
            String boundary)
            throws IOException {
        byte[] boundaryBytes = ("\r\n--" + boundary).getBytes("UTF-8");
        splitter.init(streamIn);

        // Discard everything until the first boundary, and skip the CR-LF.
        splitter.writeUntilPattern(null, boundaryBytes);
        splitter.writeBytes(null, 2);

        do {
            // Get the output stream and copy the input stream to it
            // until we find the boundary in the input stream.
            OutputStream streamOut = outputStreamGetter
                    .getStream(getHeaders(streamIn));
            splitter.writeUntilPattern(streamOut, boundaryBytes);

            // Grab the 2 bytes that follow the boundary.
            scratchPad.reset();
            splitter.writeBytes(scratchPad, 2);

            // Close the stream.
            if (streamOut != null) {
                streamOut.close();
            }

            // The two bytes will be CR-LF for normal boundaries,
            // but they will be "--" for the terminating boundary.
        } while (scratchPad.getBuf()[0] != DASH
                || scratchPad.getBuf()[1] != DASH);
    }