Java File to Byte Array getBytes(File file)

Here you can find the source of getBytes(File file)

Description

Returns the byte[] rawValue of the given File.

License

Apache License

Declaration

public static byte[] getBytes(File file) throws IOException 

Method Source Code


//package com.java2s;
/*//from w  ww.  j  a v a  2s.c  o m
 * 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.File;
import java.io.FileInputStream;

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

public class Main {
    /**
     * Returns the <code>byte</code>[] rawValue of the given <code>File</code>.
     */
    public static byte[] getBytes(File file) throws IOException {
        InputStream in = new FileInputStream(file);
        int fileSize = (int) file.length();

        if (fileSize > Integer.MAX_VALUE) {
            in.close();
            throw new IOException("File size to large: " + fileSize + " > " + Integer.MAX_VALUE);
        }

        byte[] bytes = new byte[fileSize];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            in.close();
            throw new IOException("Could not completely read file " + file.getName());
        }

        in.close();

        return bytes;
    }

    public static byte[] getBytes(InputStream in) throws Exception {
        int size = in.available();
        byte[] bytes = new byte[size];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read ");
        }

        in.close();

        return bytes;
    }
}

Related

  1. getBytes(File f)
  2. getBytes(File f)
  3. getBytes(File file)
  4. getBytes(File file)
  5. getBytes(File file)
  6. getBytes(File file)
  7. getBytes(File file)
  8. getBytes(File file)
  9. getBytes(File file)