Java File Read via ByteBuffer readHeaderArea(InputStream in)

Here you can find the source of readHeaderArea(InputStream in)

Description

read Header Area

License

Apache License

Declaration

public static byte[] readHeaderArea(InputStream in) throws IOException 

Method Source Code

//package com.java2s;
/*//ww  w . j a va  2s .com
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.nio.ByteBuffer;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    public static byte[] readHeaderArea(InputStream in) throws IOException {
        int input = 0;
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ByteBuffer last4Chars = ByteBuffer.wrap(new byte[4]);

        while ((input = in.read()) != -1) {
            byte inputByte = (byte) input;
            buffer.write(inputByte);
            last4Chars.put(inputByte);

            if (emptyLine(last4Chars)) {
                break;
            }

        }
        byte[] byteArray = buffer.toByteArray();
        return byteArray;

    }

    public static byte[] read(InputStream in, long length) throws IOException {
        int input = 0;
        long bytesRead = 0;
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        while ((input = in.read()) != -1) {
            buffer.write((byte) input);
            bytesRead++;
            if (bytesRead >= length) {
                break;
            }
        }
        return buffer.toByteArray();
    }

    private static boolean emptyLine(ByteBuffer buffer) {

        boolean foundEmptyLine = false;
        if (!buffer.hasRemaining()) {
            /*
             * empty line is either \n\n or \r\n \r\n
             */
            int noOfLf = 0;
            int noOfCr = 0;
            for (byte b : buffer.array()) {
                char ch = (char) b; // a CR or LF is exactly one byte long
                if ('\n' == ch)
                    noOfLf++;
                if ('\r' == ch)
                    noOfCr++;
            }

            if (((char) buffer.array()[3]) == '\r') {
                buffer.position(3);
                buffer.compact();
                return false;
            }

            if (noOfLf == 2 || noOfCr == 2) {
                foundEmptyLine = true;
            }

            buffer.position(2);
            buffer.compact();
        }

        return foundEmptyLine;

    }

    /**
     * COPY FROM Apache Commons Io.
     * 
     * @param input
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copy(input, output);
        return output.toByteArray();
    }

    /**
     * COPY FROM Apache Commons Io.
     * 
     * @param input
     * @param output
     * @return
     * @throws IOException
     */
    public static long copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
}

Related

  1. readFromSocket(SocketChannel channel)
  2. readFull(Reader in)
  3. readFully(final Reader input, final char[] buffer, final int offset, final int length)
  4. readFully(InputStream in)
  5. readFullyAndClose(InputStream input)
  6. readInputStream(InputStream input)
  7. readInputStreamToString(InputStream inputStream)
  8. readInt(byte[] bytes, int index)
  9. readInt(byte[] bytes, int offset, java.nio.ByteOrder byteorder)