public void close() throws IOException {
		if ( this.mBufferedOutputStream != null ) {
			boolean delete = false;
			try (BufferedInputStream input = new BufferedInputStream( new FileInputStream( this.mTempFile ) )) {
				this.mBufferedOutputStream.flush();
				this.mBufferedOutputStream.close();
				((WaveFileHeader) this.mHeader).read( input );
				((WaveFileHeader) this.mHeader).setNumBytes( this.mBytesWritten );
				createEmptyFile( this.mFile );
				delete = true;
				this.mBufferedOutputStream = new BufferedOutputStream( new FileOutputStream( this.mFile ) );
				int bytesRead = 0;
				byte[] data = new byte[BUFFER_SIZE];
				this.mHeader.write( this.mBufferedOutputStream );
				while ( (bytesRead = input.read( data, 0, BUFFER_SIZE )) != -1 ) {
					this.write( data, 0, bytesRead );
				}
			}
			catch (IOException e) {
				//only delete if a new file has been written over a possible old file
				if ( delete ) {
					if ( !this.mTempFile.delete() ) {
						throw new IOException( "couldn't delete failure file", e );
					}
				}
				throw e;
			}
			finally {
				this.mClosed = true;
				// No need to flush, is included in close().
				try {
					this.mBufferedOutputStream.close();
					this.mBufferedOutputStream = null;
				}
				finally {
					if ( !this.mTempFile.delete() ) {
						throw new IOException( "couldn't delete the tempfile" );
					}
				}
			}
		}
	}