oggWriter.close();
writer.close();
  public static void encode(String inputPath, String outputPath)    throws IOException  {    byte[] temp    = new byte[2560]; // stereo UWB requires one to read 2560b    final int HEADERSIZE = 8;    final String RIFF      = "RIFF";    final String WAVE      = "WAVE";    final String FORMAT    = "fmt ";    final String DATA      = "data";    final int WAVE_FORMAT_PCM = 0x0001;    // open the input stream    DataInputStream dis = new DataInputStream(new FileInputStream(inputPath));    // construct a new decoder    SpeexEncoder speexEncoder = new SpeexEncoder();    speexEncoder.init(mode, quality, sampleRate, channels);    if (complexity > 0) {      speexEncoder.getEncoder().setComplexity(complexity);    }    if (bitrate > 0) {      speexEncoder.getEncoder().setBitRate(bitrate);    }    if (vbr) {      speexEncoder.getEncoder().setVbr(vbr);      if (vbr_quality > 0) {        speexEncoder.getEncoder().setVbrQuality(vbr_quality);      }    }    if (vad) {      speexEncoder.getEncoder().setVad(vad);    }    if (dtx) {      speexEncoder.getEncoder().setDtx(dtx);    }    if (wav) {      // read the WAVE header      dis.readFully(temp, 0, HEADERSIZE+4);      // make sure its a WAVE header      if (!RIFF.equals(new String(temp, 0, 4)) &&          !WAVE.equals(new String(temp, 8, 4))) {        System.err.println("Not a WAVE file");        return;      }      else {//        System.out.println("Wave file size: " + readInt(temp, 4));      }      // Read other header chunks      dis.readFully(temp, 0, HEADERSIZE);      String chunk = new String(temp, 0, 4);      int size = readInt(temp, 4);      while (!chunk.equals(DATA)) {        //      System.out.println(chunk + " chunk, size: " + size);        dis.readFully(temp, 0, size);        if (chunk.equals(FORMAT)) {          /*          typedef struct waveformat_extended_tag {          WORD wFormatTag; // format type          WORD nChannels; // number of channels (i.e. mono, stereo...)          DWORD nSamplesPerSec; // sample rate          DWORD nAvgBytesPerSec; // for buffer estimation          WORD nBlockAlign; // block size of data          WORD wBitsPerSample; // Number of bits per sample of mono data          WORD cbSize; // The count in bytes of the extra size           } WAVEFORMATEX;          */          if (readShort(temp, 0) != WAVE_FORMAT_PCM) {            System.err.println("Not a PCM file");            return;          }          channels = readShort(temp, 2);          sampleRate = readInt(temp, 4);          if (readShort(temp, 14) != 16) {            System.err.println("Not a 16 bit file " + readShort(temp, 18));            return;          }        }        dis.readFully(temp, 0, HEADERSIZE);        chunk = new String(temp, 0, 4);        size = readInt(temp, 4);      }//      System.out.println("data size: " + size);    }        OggSpeexWriter oggWriter = new OggSpeexWriter();    oggWriter.setFormat(mode, sampleRate, channels, nframes);    oggWriter.open(outputPath);    oggWriter.writeHeader("Encoded with: " + VERSION);    int pcmPacketSize = 2 * channels * speexEncoder.getFrameSize();    try {      // read until we get to EOF      while (true) {        dis.readFully(temp, 0, nframes*pcmPacketSize);        for (int i=0; i<nframes; i++)          speexEncoder.processData(temp, i*pcmPacketSize, pcmPacketSize);        int encsize = speexEncoder.getProcessedData(temp, 0);        if (encsize > 0) {          oggWriter.writePacket(temp, 0, encsize);        }      }    }    catch (EOFException e) {}    oggWriter.close();   }