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 3430392
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:11:56+00:00 2026-05-18T07:11:56+00:00

before you say Google it, I tried, found a few interesting articles, but nothing

  • 0

before you say “Google it”, I tried, found a few interesting articles, but nothing worked.

I need to convert a mp3 file from a website to a byte stream, that I can later save into a file locally.

Here is my code (most important parts):

Url url = new Url("someUrl");
URLConnection conn = url.openConnection();
byte[] result = inputStreamToByteArray(conn.getInputStream());
// .... some code here
byteArrayToFile(result, "tmp.mp3");

public byte[] inputStreamToByteArray(InputStream inStream){
    InputStreamReader in = new InputStreamReader(inStream):
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int next = inStream.read();
    while (next > -1){
        baos.write(next);
        next = in.read();
    }

    byte[] result = baos.toByteArray();
    baos.flush();
    in.close();
    return result;
} 

public void byteArrayToFile(byte[] byteArray, String outFilePath){
    FileOutputStream fos = new FileOutputStream(outFilePath);
    fos.write(byteArray);
    fos.close()
}

The code compiles without errors.
Connection to the url is valid. It sends the right response.

The problem is somewhere in the conversion.
I also get the new file on disk, after byteArrayToFile(), with appropriate lenght, but I can’t play it in any player. It says length 00:00 and will not play.

Btw. I’d like to avoid any 3rd party libs. But if nothing else works…

  • 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-05-18T07:11:57+00:00Added an answer on May 18, 2026 at 7:11 am

    (The code you’ve presented wouldn’t compile without errors, by the way. You haven’t got required exception handling, and inputStream should be InputStream, just for starters.)

    This is the problem:

    InputStreamReader in = new InputStreamReader(inStream):
    

    You’re trying to read from a binary stream, and convert it into text. It isn’t text. You shouldn’t be using anything to do with “Reader” or “Writer” for binary data like an MP3 file.

    Here’s what your inputStreamToByteArray method should look like (or use Guava and its ByteStreams.toByteArray method):

    public byte[] inputStreamToByteArray(InputStream inStream) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int bytesRead;
        while ((bytesRead = inStream.read(buffer)) > 0) {
            baos.write(buffer, 0, bytesRead);
        }
        return baos.toByteArray();
    }
    

    Note that I’ve left it for the caller to clean up the input stream. Typically whoever fetches the stream also closes it – it would be slightly odd for a method like this to close it itself, IMO. You could do so if you wanted to, but probably in a finally block so you’d know that it would always be closed even if an exception was thrown.

    Note that your “writing” code later on won’t close the file if there’s an exception, either. You should get in the habit of always closing streams in finally blocks.

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

Sidebar

Related Questions

Now, before you say it: I did Google and my hbm.xml file is an
Probably this question was already asked before, but my google-fu and SO-Search did not
I've read up a good few articles etc., here and from Google, about trying
I'm participating in google code jam. Before anything I want to say that I
First of all, I must say that I'm very new to Google Closure, but
Let's say before a user is allowed to view the creation page of a
Read on before you say this is a duplicate, it's not. (as far as
Before someone said that I did not read I may say that I read
Let's say I'd like to start a small linux distro before my ordinary operating
In xquery, How to get 90 days before date from a given date? Say

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.