copy InputStream Until Crlf Crlf - Java java.io

Java examples for java.io:InputStream Read

Description

copy InputStream Until Crlf Crlf

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    private static final int CRLF_CRLF = ('\r' << 24) | ('\n' << 16)
            | ('\r' << 8) | ('\n');

    public static int copyUntilCrlfCrlf(InputStream in, OutputStream out)
            throws IOException {
        int byteCount = 0;
        int fourBytes = 0;
        for (;;) {
            int b = in.read();
            if (b == -1) {
                break;
            }/*  w ww . j  av a 2 s.c  o m*/
            b &= 0xFF;

            out.write(b);
            ++byteCount;

            fourBytes = (fourBytes << 8) | b;
            if (fourBytes == CRLF_CRLF) {
                return byteCount;
            }
        }

        return -byteCount;
    }
}

Related Tutorials