我应该如何将mp3正确转换为字节数组,然后再转换回mp3

问题描述 投票:0回答:1

我正在制作一个项目,其中一部分涉及使用TCP将mp3文件从客户端传输到服务器。我的想法是,客户端将通过FileInputStream将mp3转换为字节数组,并且连接到套接字的输出流会将字节数组传递给服务器。服务器将通过套接字的输入流获取字节数组,并通过FileOutputStream将其转换回mp3文件。

但是,我有两个问题。首先,程序运行正常,但是服务器转换的最终mp3文件是一个仅包含1个字节的空文件,而FileOutputStream接收和使用的字节数组不为空。我知道当服务器尝试将字节数组转换回mp3时肯定有问题,因为仅使用FileOutputStream似乎太容易了,我可能误解了其功能,因此我想知道如何正确地从套接字接收字节数组。

其次,我试图比较两个程序中的字节数组,但发现它们是不同的。它们的一部分是相同的,但是它们中的大多数是不同的,尤其是字节数组的开头和结尾,我不确定为什么。我在使用套接字上的InputStream和OutputStream时是否存在概念性问题?

这里是发送mp3的客户端代码的一部分:

public static void sendPackets(){
        System.out.println("Sending test file...");
        try{
            while (active){
                File file = new File("Sorrow.mp3"); // Sorrow.mp3 is the local mp3 music needs to be sent
                FileInputStream loc = new FileInputStream(file);
                sendData = new byte[(int)file.length()];
                loc.read(sendData);
                //socket_tcp is a Socket object connecting to the server
                OutputStream fis = socket_tcp.getOutputStream(); 
                fis.write(sendData);
                fis.flush();
                fis.close();
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这是服务器的代码,它接收字节数组并将其转换回mp3:

/** This is the function for the thread listening and receiving the music
  * active is a boolean value tracking whether the server is suppose to keep running or not
 **/

public static void listen() {
        while (active) {
            try {
                //Wait until packet is received
                // listenSocket is a ServerSocket specific for this thread
                socket = listenSocket.accept();
                System.out.println("We got music from the client!");
                File file = new File("Song.mp3");
                InputStream is = socket.getInputStream();
                receiveData = new byte[1024];
                is.read(receiveData);
                System.out.println(Arrays.toString(receiveData));
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(receiveData);
                fos.flush();
                fos.close();

            } catch (IOException e) {
                if(active) {
                    listen();
                } else {
                    break;
                }
            }
        }

我是这个社区的新手,所以如果我在提问时犯了任何错误,请告诉我,谢谢!任何帮助表示赞赏!

java sockets audio tcp client-server
1个回答
0
投票

我没有尝试调试您的确切问题,但是我有一些评论。

并非严格需要缓冲区

在将文件写入输出流之前将其读入缓冲区是一种速度优化。您可以通过读取InputStream并直接将字节逐字节写入OutputStream来简化代码(至少从头开始)。一旦有了更简单的版本,然后发现缓冲区性能不佳,可以引入一个缓冲区。实际上,使用缓冲区可能不会有太大的区别,因为操作系统本身会进行很多抢先式缓存。

read()应该在循环中

read()上的InputStream方法返回读取的字节总数。不能保证一次性读取所有字节。您应该始终将read()放入循环中,并将返回的偏移量作为数组的偏移量用于下一次读取,直到获得-1表示文件结束为止。

byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = input.read(buffer)) != -1) {
    output.write(buffer, 0, bytesRead);
}

尝试使用资源

为了确保即使抛出异常也可以释放资源,您应该使用try..finally块,或者甚至更好的是使用try-with-resources块为您调用close()

示例:

FileOutputStream fos = new FileOutputStream(file);
try {
    fos.write(receiveData);
} finally {
    fos.close();
}

或更简单的等价于try-with-resources块:

try (FileOutputStream fos = new FileOutputStream(file)) {
    fos.write(receiveData);
}

不需要flush()

不需要flush()close()。后者会在关闭流之前自动刷新。

© www.soinside.com 2019 - 2024. All rights reserved.