开发者

How to write RAW data to a file using Java? e.g same as: nc -l 8000 > capture.raw

开发者 https://www.devze.com 2023-04-11 04:19 出处:网络
In TCP i am receiving media stream from an IP camera as RAW. According to there advise, i need to write that as file. And then i can play it with media player such as VLC.

In TCP i am receiving media stream from an IP camera as RAW. According to there advise, i need to write that as file. And then i can play it with media player such as VLC.

But when i write this to a file, and play with media players it never play corrupted.

After comparing the original file i see my Java writing it in wrong characters. And there sample file shows different. What or how do i fix such file writing issue, here is how i am writing it:

byte[] buf=new byte[1024];
int bytes_read = 0;
try {  
    bytes_read = sock.getInputStream().read(buf, 0, buf.length);                
    String data = new String(buf, 0, bytes_read);                   
    System.err.println("DATA: " +  bytes_read + " bytes, data=" +data);

        BufferedWriter out = new BufferedWriter(
  开发者_如何学Python          new FileWriter("capture.ogg", true));
        out.write(data);
        out.close();

} catch (IOException e) {
    e.printStackTrace(System.err);
}


You shouldn't use Readers, Writers and Strings for binary data. Stick with InputStreams and OutputStreams.

I.e., change

  • BufferedWriter -> BufferedOutputStream,
  • FileWriter -> FileOutputStream
  • and instead of String, just use a byte[].

If you're dealing with sockets, I must advice you to look into the NIO package though.


You're doing it right... at least until the part where you turn your byte[] into a String:

That step only really makes sense if your byte[] represents textual data in the first place! Which it doesn't!

Whenever you handle binary data or don't actually care what the data represents you must avoid using String/Reader/Writer to handle that data. Instead do use byte[]/InputStream/OutputStream.

Also, you must read from the socket in a loop, because nothing guarantees that you've read everything:

byte[] buf=new byte[1024];
int bytes_read;
OutputStream out = new FileOutputStream("capture.ogg", true);
InputStream in = sock.getInputStream();
while ((bytes_read = in.read(buf)) != -1) {
    out.write(buf, 0, bytes_read);
}
out.close();


The way you have it written limits the output file to a maximum size of 1024 bytes. Try a loop:

    try {
        byte[] buf = new byte[1024];
        int bytes_read = 0;
        InputStream in = sock.getInputStream();
        FileOutputStream out = new FileOutputStream(new File("capture.ogg"));

        do {
            bytes_read = in.read(buf, 0, buf.length);
            System.out.println("Just Read: " + bytes_read + " bytes");

            if (bytes_read < 0) {
                /* Handle EOF however you want */
            }

            if (bytes_read > 0)
                  out.write(buf, 0, bytes_read);

        } while (bytes_read >= 0);

        out.close();

    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消