I am trying to read an Mp3 file from USB and write the data into different buffers. Please help me if there's any good way to do this.
I could read the whole data into a byte array. But i want to do something like writing the first 8 byte into first byte array and next 8 byte into second byte array and so on till end of the data. Please provide a sample code if possible. As i can grasp very fast if i have any example in front of me.
Below is my code which i have to read the data.
public class ReadFileInByteArrayWithFileInputStream {
static byte[] buffer1 ;
@SuppressWarnings("null")
public static void main() {
File file = new File("/mnt/media/wayfaring_stranger.mp3");
FileInputStream fin = null;
FileOutputStream fout=null;
try
{
// create FileInputStream object
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int) file.length()];
// Reads up to certain bytes of data from this input stream into an
// array of bytes.
fin.read(fileContent);
// create string from byte array
String s = new String(fileContent);
System.out.println("File content: " + s);
buffer1 = new byte[8];
}
catch (FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch (IOException ioe) {
System.out.println("Exception while reading file " + ioe);
}
finally {
// close the streams using close method
try
{
if (fin != null) {
fin.close();
}
}
catch (IOException ioe)
{
System.out.println("Error while closing stream: " + ioe);
}
}
}
}
Firstly, splitting a large file into buffers that small will be incredibly wasteful of memory. The overhead for each object will be about the same size (or greater than) the data itself. However, the idea of splitting an input stream into multiple chunks is not inherently problematic.
I think the concept you're missing is using a collection of buffers rather than having a separate variable for each one. So instead of having buffer1
, buffer2
, buffer3
etc each of which is a byte[]
variable, you'd have a single List<byte[]>
variables called buffers
(or whatever).
Here's some code to do the reading:
public static List<byte[]> readBuffers(InputStream input, int maxBufferSize)
throws IOException {
List<byte[]> ret = new ArrayList<byte[]>();
while (true) {
byte[] buffer = new byte[maxBufferSize];
int bufferRead = 0;
while (bufferRead < buffer.length) {
int chunk = input.read(buffer, bufferRead, buffer.length - bufferRead);
if (chunk == -1) {
// End of data.
// If there's no data in this buffer, our list is fine. Otherwise
// we need to create a smaller buffer and add that.
if (bufferRead != 0) {
byte[] finalBuffer = new byte[bufferRead];
System.arraycopy(buffer, 0, finalBuffer, 0, bufferRead);
ret.add(finalBuffer);
}
return ret;
}
}
// Full buffer. Add it to the list, and start again.
ret.add(buffer);
}
}
Note how:
InputStream.read
. Never ignore it, as it's not guaranteed that it will read as much data as you asked for in a single call.String
constructor - and if you are reading bytes which were originally text data, you should specify the encoding.Alternatively, if you're actually just trying to pass each chunk of the file to some other function - for hashing, for example - you don't need to have the whole file in memory at all. Instead, you'd encapsulate the idea of "a chunk processor" and pass it into the method. Where I'm adding to a list in the method above, you'd call the chunk processor instead.
See more on this question at Stackoverflow