title: Converting byte stream to .wav file
body: <p>I am trying to make a program which sends a file from a server to a client (via sockets). The server converts a .wav file to a byte array and sends that to the client. My problem though is that I can't play the .wav file (at the client) because it is corrupted. I have done some research and found out that for this you can't just use a FileOutputStream or BufferedOutputStream. I just don't know how to convert the byte array to the .wav file so it can be played. Maybe someone knows the answer for me?</p>
  
  <p>Server code:</p>
  
  <pre><code>package me.Ciaran.simplefileserver;
  
  import java.io.BufferedInputStream;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class SimpleFileServer {
  
  public final static int SOCKET_PORT = 13267;  // you may change this
  public final static String FILE_TO_SEND = "D:/server/plugins/TBMCAudioServer/music/DLTALL.wav";  // you may change this
  
  public static void main (String [] args ) throws IOException {
  FileInputStream fis = null;
  BufferedInputStream bis = null;
  OutputStream os = null;
  ServerSocket servsock = null;
  Socket sock = null;
  try {
    servsock = new ServerSocket(SOCKET_PORT);
    while (true) {
      System.out.println("Waiting...");
      try {
        sock = servsock.accept();
        System.out.println("Accepted connection : " + sock);
        // send file
  
        final File myFile= new File(FILE_TO_SEND); //sdcard/DCIM.JPG
        byte[] mybytearray = new byte[8192];
        fis = new FileInputStream(myFile);  
        bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);
        try {
            os = sock.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            dos.writeUTF(myFile.getName());     
            dos.writeLong(mybytearray.length);
            int read;
            while((read = dis.read(mybytearray)) != -1){
                dos.write(mybytearray, 0, read);
            }
  
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  
        System.out.println("Done.");
      }
      finally {
        if (bis != null) bis.close();
        if (os != null) os.close();
        if (sock!=null) sock.close();
      }
    }
  }
  finally {
    if (servsock != null) servsock.close();
  }
  }
  }
  </code></pre>
  
  <p>and the client code:</p>
  
  <pre><code>package me.Ciaran.simplefileclient;
  
  import java.io.BufferedOutputStream;
  import java.io.DataInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.Socket;
  
  public class SimpleFileClient {
  
  public final static int SOCKET_PORT = 13267;      // you may change this
  public final static String SERVER = "127.0.0.1";  // localhost
  public final static String
     FILE_TO_RECEIVED = "C:/Users/Ciaran/Documents/TESTEN/music/DLTALL.wav";  // you may change this, I give a
                                                          // different name because i don't want to
                                                          // overwrite the one used by server...
  
  public final static int FILE_SIZE = 6022386; // file size temporary hard coded
                                             // should bigger than the file to be downloaded
  
  @SuppressWarnings({ "resource", "unused" })
  public static void main (String [] args ) throws IOException {
  int bytesRead;
  int current = 0;
  FileOutputStream fos = null;
  BufferedOutputStream bos = null;
  Socket sock = null;
  try {
    sock = new Socket(SERVER, SOCKET_PORT);
    System.out.println("Connecting...");
  
    // receive file
  
    InputStream in;
    int bufferSize=0;
  
    try {
        bufferSize=sock.getReceiveBufferSize();
        in=sock.getInputStream();
        DataInputStream clientData = new DataInputStream(in);
        String fileName = clientData.readUTF();
        System.out.println(fileName);
        OutputStream output = new FileOutputStream(FILE_TO_RECEIVED);
        byte[] buffer = new byte[bufferSize];
        int read;
        while((read = clientData.read(buffer)) != -1){
            output.write(buffer, 0, read);
        }
  
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  
    System.out.println("File " + FILE_TO_RECEIVED
        + " downloaded (" + current + " bytes read)");
  }
  finally {
    if (fos != null) fos.close();
    if (bos != null) bos.close();
    if (sock != null) sock.close();
  }
  }
  }
  </code></pre>
  
  <p>Thanks for helping me!</p>
  
  <p>EDIT: I have compared two music files online and I found out that Java added a ton of whitespaces at the beginning of the file. That is why it won't run. Anyone know how to fix that?</p>