Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:39:19+00:00 2026-06-10T23:39:19+00:00

I need to convert a file to a byte array, but I can’t do

  • 0

I need to convert a file to a byte array, but I can’t do it with a .mov file (1.32 Gb). The method works fine with small .txt files and JPEG images, but when I try to do it with the MOV file I can’t. I just get a NullPointerException.

code:

public byte[] fileToByteArray(File file){
    ByteArrayOutputStream baos = null;
    InputStream fis = null;
    try{
        byte[] buffer = new byte[(int)file.length()];
        baos = new ByteArrayOutputStream();
        fis = new FileInputStream(file);
        int read;
        while((read = fis.read(buffer)) != -1){
            baos.write(buffer, 0, read);
        }
    }catch(Exception ex){
        System.out.println("Error: 1");
    }finally{
        try{
            if(baos != null){
                baos.close();
            }
        }catch(Exception ex){
            System.out.println("Error: 2");
        }
        try{
            if(fis != null){
                fis.close();
            }
        }catch(Exception ex){
            System.out.println("Error: 3");
        }
        return baos.toByteArray();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:39:21+00:00Added an answer on June 10, 2026 at 11:39 pm

    In this line:

    return baos.toByteArray();
    

    You’re unconditionally dereferencing baos… even though it will still be null if this line throws an exception:

    byte[] buffer = new byte[(int)file.length()];
    

    I suspect that line has thrown an exception (out of memory, at a guess)… but due to your exception handling strategy of “catch all exceptions, print them out and ignore them” you’re keeping going when you shouldn’t.

    The immediate fix to this is to only catch the exceptions you really want to catch – which should almost certainly only be IOException in this case, if anything. In fact, I probably wouldn’t use any catch clauses in the code you’ve written at all, other than possibly on the close code. (You don’t want an exception in close to obscure an exception elsewhere. If you’re using Java 7, use the try-with-resources statement to make all of this easier to start with.)

    “Handling” exceptions in the way you’re doing is a very bad idea – you’re basically saying that you want to proceed regardless of what’s gone wrong, and that just makes one error lead to another if you’re lucky, or if you’re unlucky you end up making bad state changes, e.g. overwriting good data with bad, without even realizing it.

    Now as for the bigger problem – you’re trying to allocate an enormous array, and I suspect you haven’t told the JVM to use that much memory. You’ll need to start it with something like:

    java -Xmx3G ...
    

    … assuming you’re on a 64-bit JVM which is happy to use that much memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bin file that I need to convert to a byte array.
I received multiple TTF font files. Need to convert into 1 file which I
I need to convert image sequences(ie,png) to video file in iPhone. How i can
I have a large .txt file of records that I need to convert into
If I give the decrypter RSAalg2.Decrypt(encryptedData, false); it works fine but I need to
I'm writing a Binary file converter in which I need to convert 1-6 byte
I have a number of byte[] array variables I need to convert to string
I need to convert a byte array to a UTF8 string, and preserve the
Is it possible to convert a file to a byte array and save it
I have an situation where i need to convert Doc file into PDF file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.