PcmWavConverter { public static void convertPCMToWav(WaveHeader waveHeader,final String pcmFilePath, final String wavFilePath)throws IOException{ if (waveHeader ==null){ throw new IllegalArgumentException("waveHeader cannot be null"); } if (pcmFilePath==null || pcmFilePath.trim().isEmpty()){ throw new IllegalArgumentException("pcmFilePath cannot be null, empty, blank"); } final File pcmFile=new File(pcmFilePath); if (wavFilePath==null || wavFilePath.trim().isEmpty()){ throw new IllegalArgumentException("wavFilePath cannot be null, empty, blank"); } final DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(wavFilePath))); final DataInputStream dataInputStream=new DataInputStream(new BufferedInputStream(new FileInputStream(pcmFile))); try { writeWavHeader(waveHeader,dataOutputStream,new File(pcmFilePath)); writePCMData(dataOutputStream, dataInputStream); } finally { dataOutputStream.flush(); dataOutputStream.close(); dataInputStream.close(); } final File partWavFile=new File(wavFilePath); if (!partWavFile.renameTo(new File(wavFilePath))){ throw new IOException("Unable to rename file to:"+wavFilePath); } }  static void addWavHeader(WaveHeader waveHeader,final String wavFilePath); static void convertPCMToWav(WaveHeader waveHeader,final String pcmFilePath, final String wavFilePath); static final long MAX_TIME_WAV_FILE_MILLIS; static final long MAX_SIZE_WAV_FILE_BYTES; }
@Test(expected=IllegalArgumentException.class) public void testNullWaveHeader()throws IOException{PcmWavConverter.convertPCMToWav(null, "/x/something/x/Gavin.pcm", "/x/something/x/Gavin.wav");}
@Test(expected=IllegalArgumentException.class) public void testNullPCMPath()throws IOException{PcmWavConverter.convertPCMToWav(waveHeader,null,"/x/something/x/Gavin.wav");}
@Test(expected=IllegalArgumentException.class) public void testEmptyPCMPath()throws IOException{PcmWavConverter.convertPCMToWav(waveHeader, "", "/x/something/x/Gavin.wav");}
@Test(expected=IllegalArgumentException.class) public void testBlankPCMPath()throws IOException{PcmWavConverter.convertPCMToWav(waveHeader," ","/x/something/x/Gavin.wav");}
@Test(expected=IllegalArgumentException.class) public void testNullWAVPath()throws IOException{PcmWavConverter.convertPCMToWav(waveHeader,"/x/something/x/Gavin.pcm",null);}
@Test(expected=IllegalArgumentException.class) public void testEmptyWAVPath()throws IOException{PcmWavConverter.convertPCMToWav(waveHeader,"/x/something/x/Gavin.pcm","");}
@Test(expected=IllegalArgumentException.class) public void testBlankWAVPath()throws IOException{PcmWavConverter.convertPCMToWav(waveHeader,"/x/something/x/Gavin.pcm"," ");}
@Test public void testPcmWavConversion()throws IOException{ final String pcmFilePath=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Test"+ File.separator+"Test.pcm"; createPCMFile(pcmFilePath); final String wavFilePath=pcmFilePath.replace(".pcm", ".wav"); PcmWavConverter.convertPCMToWav(waveHeader, pcmFilePath, wavFilePath); final DataInputStream dataInputStream=new DataInputStream(new BufferedInputStream(new FileInputStream(wavFilePath))); byte[] expectedByteArray=new byte["RIFF".getBytes().length]; dataInputStream.read(expectedByteArray); assertArrayEquals("RIFF is not there or in correct position.", "RIFF".getBytes(), expectedByteArray); Assert.assertEquals("File length is not there or in correct position", Integer.reverseBytes(36 + (int) new File(pcmFilePath).length()), dataInputStream.readInt()); expectedByteArray=new byte["WAVE".getBytes().length]; dataInputStream.read(expectedByteArray); assertArrayEquals("WAVE is not there or in correct position.", "WAVE".getBytes(), expectedByteArray); expectedByteArray=new byte["fmt ".getBytes().length]; dataInputStream.read(expectedByteArray); assertArrayEquals("fmt is not there or in correct position.", "fmt ".getBytes(), expectedByteArray); Assert.assertEquals("16 is not there or in correct position.", Integer.reverseBytes(16), dataInputStream.readInt()); Assert.assertEquals("PCM format is not there or in correct position.", Short.reverseBytes((short) 1), dataInputStream.readShort()); Assert.assertEquals("Channel num is not there or in correct position.", Short.reverseBytes(waveHeader.getChannelNum()), dataInputStream.readShort()); Assert.assertEquals("Sample rate is not there or in correct position.",Integer.reverseBytes(waveHeader.getSampleRateInHertz()),dataInputStream.readInt()); Assert.assertEquals("Data calculation is not there or in correct position.",Integer.reverseBytes(waveHeader.getChannelNum() * waveHeader.getSampleRateInHertz() * waveHeader.getBitRate() / 8),dataInputStream.readInt()); Assert.assertEquals("Channel num and bit rate calculation is not there or in correct position.", Short.reverseBytes((short) (waveHeader.getChannelNum() * waveHeader.getBitRate() / 8)),dataInputStream.readShort()); Assert.assertEquals("Bit rate is not there or in correct position",Short.reverseBytes(waveHeader.getBitRate()),dataInputStream.readShort()); expectedByteArray=new byte["data".getBytes().length]; dataInputStream.read(expectedByteArray); assertArrayEquals("data is not there or in correct position.", "data".getBytes(), expectedByteArray); Assert.assertEquals("File size is not there or in correct position.",Integer.reverseBytes((int) new File(pcmFilePath).length()),dataInputStream.readInt()); int i=0; while (dataInputStream.available()>0) { ByteBuffer bb = ByteBuffer.allocate(2); bb.order(ByteOrder.LITTLE_ENDIAN); bb.put(dataInputStream.readByte()); bb.put(dataInputStream.readByte()); Assert.assertEquals(pcmDataSample[i++],bb.getShort(0)); } }