Reading a binary input stream into a single byte array in Java

The simplest approach IMO is to use Guava and its ByteStreams class: byte[] bytes = ByteStreams.toByteArray(in); Or for a file: byte[] bytes = Files.toByteArray(file); Alternatively (if you didn’t want to use Guava), you could create a ByteArrayOutputStream, and repeatedly read into a byte array and write into the ByteArrayOutputStream (letting that handle resizing), then call … Read more

How to merge two mp3 files into one (combine/join)

import java.io.*; public class TwoFiles { public static void main(String args[]) throws IOException { FileInputStream fistream1 = new FileInputStream(“C:\\Temp\\1.mp3”); // first source file FileInputStream fistream2 = new FileInputStream(“C:\\Temp\\2.mp3”);//second source file SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2); FileOutputStream fostream = new FileOutputStream(“C:\\Temp\\final.mp3”);//destinationfile int temp; while( ( temp = sistream.read() ) != -1) { // System.out.print( (char) … Read more

How to store large blobs in an android content provider?

The solution phreed gives in the bottom half of question is basically correct. I’ll try to add some more details here. When you do getContentResolver().openInputStream(…), content resolver will go to your content provider and call its openFile method. This is how the openFile looks in ContentProvider.java: public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { … Read more

Determine the size of an InputStream

This is a REALLY old thread, but it was still the first thing to pop up when I googled the issue. So I just wanted to add this: InputStream inputStream = conn.getInputStream(); int length = inputStream.available(); Worked for me. And MUCH simpler than the other answers here. Warning This solution does not provide reliable results … Read more

InputStream from a URL

Use java.net.URL#openStream() with a proper URL (including the protocol!). E.g. InputStream input = new URL(“http://www.somewebsite.com/a.txt”).openStream(); // … See also: Using java.net.URLConnection to fire and handle HTTP requests