public int read(byte b[], int off, int len) throws IOException {
    if (available() < len) {
      copyEvents(len);
    }

    return stream.getInputStream().read(b, off, len);
  }
  public int read() throws IOException {
    if (available() == 0) {
      copyEvents(0x10000);
    }

    return stream.getInputStream().read();
  }
  private void copyEvents(int length) throws IOException {
    try {
      while (reader.hasNext() && stream.getInputStream().available() < length) {
        writer.add(reader.nextEvent());
      }

      writer.flush();
    } catch (XMLStreamException e) {
      throw new be.re.io.IOException(e);
    }
  }
 public int available() throws IOException {
   return stream.getInputStream().available();
 }