This discussion is archived
1 2 Previous Next 27 Replies Latest reply: Feb 17, 2013 4:32 PM by EJP RSS

Need an example of read a file into ByteArrayInputStream

Aacc Newbie
Currently Being Moderated
Could someone give an example of read a file into ByteArrayInputStream. thanks
I'm using JDK 6
  • 1. Re: Need an example of read a file into ByteArrayInputStream
    EJP Guru
    Currently Being Moderated
    while ((count = in.read(buffer)) > 0)
    {
        out.write(buffer, 0, count);
    }
    out.close();
  • 2. Re: Need an example of read a file into ByteArrayInputStream
    Aacc Newbie
    Currently Being Moderated
    Thank you for replying my post. I was asking for "read a file into ByteArrayInputStream"
    but I don't see any "File" and "ByteArrayInputStream" in your replying. is this for my original question?

    Thanks anyway.
  • 3. Re: Need an example of read a file into ByteArrayInputStream
    Kayaman Guru
    Currently Being Moderated
    Aacc wrote:
    Thank you for replying my post. I was asking for "read a file into ByteArrayInputStream"
    but I don't see any "File" and "ByteArrayInputStream" in your replying. is this for my original question?
    First of all, you can't read a file into a ByteArrayInputStream. InputStreams are where you read from, not where you write to. What you want to do is to read a file into a byte array.
    From there you can create a ByteArrayInputStream. Or you can use a combination of ByteArrayOutputStream and ByteArrayInputStream.

    But streams work pretty much all the same, there's millions of examples on the web, so if you can't find one with Google, you better just give up programming.
  • 4. Re: Need an example of read a file into ByteArrayInputStream
    EJP Guru
    Currently Being Moderated
    Thank you for replying my post. I was asking for "read a file into ByteArrayInputStream"
    but I don't see any "File" and "ByteArrayInputStream" in your replying. is this for my original question?
    You are correct. I made the mistake of assuming you meant ByteArrayOutputStream, as you can't read anything into a ByteArrayInputStream, let alone a file. I also assumed that you could see that the code applies to any input stream and any output stream, including a FileInputStream and a ByteArrayOutputStream. However using a FileInputStream means that you don't need a ByteArrayInputStream at all. So the actual point and intent of your question remains a mystery.
  • 5. Re: Need an example of read a file into ByteArrayInputStream
    Aacc Newbie
    Currently Being Moderated
    I'm invoking a 3rd party method which takes a InputStream as parameter. however, I realized that passing in a regular InputStream causing exception that " java.io.IOException: Resetting to invalid mark" .
    I'm not able to make change to this 3rd party method. therefore, I found: http://www.coderanch.com/t/535208//java/BufferedInputStream-set-method
    it suggested to use ByteArrayInputStream which works fine.

    however, in my case, i have to convert a File into this ByteArrayInputStream.

    I've made the following progress so far that I can convert from File into this ByteArrayInputStream (myFile is a File object):
                    InputStream ins = new FileInputStream(myFile);
                    BufferedInputStream bfins = new BufferedInputStream(ins);
                    int fileSize = (int)myFile.length();
                    byte[] blob = new byte[fileSize];
                    int bytes_read = 0;
                    int offset = 0;
    
                    while ((bytes_read = bfins.read(blob, 0, fileSize)) != -1) {
                        offset += bytes_read;
                    }
    
                    ByteArrayInputStream bins = new ByteArrayInputStream(blob);
    however, as you can see, I had to down cast the fileSize like int fileSize = (int)myFile.length(); which means this does not work with large file and have to load everything in memory.
    is there better way to convert from File to ByteArrayInputStream ?
  • 6. Re: Need an example of read a file into ByteArrayInputStream
    DrClap Expert
    Currently Being Moderated
    What, you were hoping for a ByteArrayInputStream which would somehow not be in memory?

    I suggest you stop messing with ByteArrayInputStream and go back and look for a better solution to your original problem, whatever it was.
  • 7. Re: Need an example of read a file into ByteArrayInputStream
    Aacc Newbie
    Currently Being Moderated
    OK, what is the best way solve this original problem :
    +"I'm invoking a 3rd party method which takes a InputStream as parameter. however, I realized that passing in a regular InputStream causing exception that " java.io.IOException: Resetting to invalid mark" .+
    +I'm not able to make change to this 3rd party method."+

    Thanks
  • 8. Re: Need an example of read a file into ByteArrayInputStream
    jtahlborn Expert
    Currently Being Moderated
    Aacc wrote:
    while ((bytes_read = bfins.read(blob, 0, fileSize)) != -1) {
    offset += bytes_read;
    }
    this will get you a nice jumbled bit of the file. i suggest incorporating the "offset" into the read() method call.

    related to your actual problem, do you know why the third-party method calls reset on the stream? does it need to re-read the whole thing, or a snippet, or just calls it for the heck of it?

    (as a future side note, i would suggest asking questions about your actual problem, not about what you think the solution might be. it often leads to better solutions faster)
  • 9. Re: Need an example of read a file into ByteArrayInputStream
    DrClap Expert
    Currently Being Moderated
    Pass in an InputStream which supports the mark() and reset() method. (Read the API documentation if you didn't know they existed.) If yours doesn't, or if its buffer isn't big enough, wrap it in a PushbackInputStream with a sufficiently large buffer.
  • 10. Re: Need an example of read a file into ByteArrayInputStream
    Aacc Newbie
    Currently Being Moderated
    Could you please how to " incorporating the "offset" into the read()" and how would it help with the issue?
    Sorry, I have no much idea about the 3rd party method, I just saw how it is.
    Thanks
  • 11. Re: Need an example of read a file into ByteArrayInputStream
    jtahlborn Expert
    Currently Being Moderated
    Aacc wrote:
    Could you please how to " incorporating the "offset" into the read()" and how would it help with the issue?
    Sorry, I have no much idea about the 3rd party method, I just saw how it is.
    so, you're not sure what the problem is you are trying to solve, just trying to throw some code at it until it works?
  • 12. Re: Need an example of read a file into ByteArrayInputStream
    DrClap Expert
    Currently Being Moderated
    Aacc wrote:
    Could you please how to " incorporating the "offset" into the read()" and how would it help with the issue?
    No, because (a) I never said that in the first place, and (b) I have no idea what it means.
  • 13. Re: Need an example of read a file into ByteArrayInputStream
    EJP Guru
    Currently Being Moderated
    however, in my case, i have to convert a File into this ByteArrayInputStream.
    Why? Bad idea. You don't want to hold entire files in memory. It doesn't scale.
    I've made the following progress so far that I can convert from File into this ByteArrayInputStream (myFile is a File object):
    Not with that code you can't. It doesn't work. You haven't made any progress at all. You have to copy it to a ByteArrayOutputStream, using the code I already gave you, then construct a new ByteArrayInputStream using the array you get from the ByteArrayOutputStream.
    is there better way to convert from File to ByteArrayInputStream ?
    See above.
  • 14. Re: Need an example of read a file into ByteArrayInputStream
    Aacc Newbie
    Currently Being Moderated
    So, I guess it would be something like:
    BufferedInputStream bis = new BufferedInputStream(
            new FileInputStream("somefile"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    
    while ((count = bis.read(buffer)) > 0)
    {
        baos.write(buffer, 0, count);
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray() ); 
     
    would this baos.toByteArray() stilll need to hold entire file in memory?

    Thanks.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points