如何使用Java将RAW数据写入文件?例如,与: nc -l 8000 > 相同捕获.raw

发布于 2024-12-08 12:15:41 字数 700 浏览 1 评论 0原文

在 TCP 中,我从 IP 摄像机接收 RAW 媒体流。根据那里的建议,我需要将其写为文件。然后我可以用 VLC 等媒体播放器播放它。

但是当我将其写入文件并使用媒体播放器播放时,它永远不会播放损坏。

比较原始文件后,我发现我的 Java 用了错误的字符。并且示例文件显示不同。我该如何解决此类文件写入问题,以下是我的编写方式:

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(
            new FileWriter("capture.ogg", true));
        out.write(data);
        out.close();

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

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(
            new FileWriter("capture.ogg", true));
        out.write(data);
        out.close();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦情居士 2024-12-15 12:15:41

您不应将 ReadersWritersStrings 用于二进制数据。坚持使用InputStreamsOutputStreams

即,更改

  • BufferedWriter -> BufferedOutputStream
  • FileWriter -> FileOutputStream
  • ,而不是String,只需使用byte[]

如果您正在处理套接字,我必须建议您查看 NIO 包 不过。

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.

躲猫猫 2024-12-15 12:15:41

你做得对......至少直到你将 byte[] 转换为 String 的部分:

只有当你的 byte[] 首先表示文本数据!它没有

每当您处理二进制数据实际上并不关心数据代表什么时,您必须避免使用String/< code>Reader/Writer 来处理该数据。相反,请使用 byte[]/InputStream/OutputStream

另外,您必须循环地从套接字读取数据,因为没有什么可以保证您已经读取了所有内容:

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();

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();
古镇旧梦 2024-12-15 12:15:41

您的写入方式将输出文件的最大大小限制为 1024 字节。尝试循环:

    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);
    }

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文