// @Override
  @Override
  public int read() throws IOException {
    // TODO: how do we detect IOException?
    fillBuffer();
    if (buffer.getLength() == 0 && buffer.isEOM()) // TODO: will always be
      // EOM if length is 0
      return -1;
    final byte[] data = (byte[]) buffer.getData();
    final int result = data[buffer.getOffset()] & 0xff;
    buffer.setOffset(buffer.getOffset() + 1);
    buffer.setLength(buffer.getLength() - 1);

    return result;
  }
    public VideoTrack(PullSourceStream stream) throws ResourceUnavailableException {
      super();

      this.stream = stream;
      // set format

      // read first frame to determine format
      final Buffer buffer = new Buffer();
      readFrame(buffer);
      if (buffer.isDiscard() || buffer.isEOM())
        throw new ResourceUnavailableException("Unable to read first frame");
      // TODO: catch runtime exception too?

      // parse jpeg
      final java.awt.Image image;
      try {
        image =
            ImageIO.read(
                new ByteArrayInputStream(
                    (byte[]) buffer.getData(), buffer.getOffset(), buffer.getLength()));
      } catch (IOException e) {
        logger.log(Level.WARNING, "" + e, e);
        throw new ResourceUnavailableException("Error reading image: " + e);
      }

      if (image == null) {
        logger.log(Level.WARNING, "Failed to read image (ImageIO.read returned null).");
        throw new ResourceUnavailableException();
      }

      if (frameContentType.equals("image/jpeg"))
        format =
            new JPEGFormat(
                new Dimension(image.getWidth(null), image.getHeight(null)),
                Format.NOT_SPECIFIED,
                Format.byteArray,
                -1.f,
                Format.NOT_SPECIFIED,
                Format.NOT_SPECIFIED);
      else if (frameContentType.equals("image/gif"))
        format =
            new GIFFormat(
                new Dimension(image.getWidth(null), image.getHeight(null)),
                Format.NOT_SPECIFIED,
                Format.byteArray,
                -1.f);
      else if (frameContentType.equals("image/png"))
        format =
            new PNGFormat(
                new Dimension(image.getWidth(null), image.getHeight(null)),
                Format.NOT_SPECIFIED,
                Format.byteArray,
                -1.f);
      else
        throw new ResourceUnavailableException(
            "Unsupported frame content type: " + frameContentType);
      // TODO: this discards first image. save and return first time
      // readFrame is called.

    }
  // @Override
  @Override
  public int read(byte[] b, int off, int len) throws IOException {
    // TODO: how do we detect IOException?
    fillBuffer();
    if (buffer.getLength() == 0 && buffer.isEOM()) // TODO: will always be
      // EOM if length is 0
      return -1;
    final byte[] data = (byte[]) buffer.getData();

    int lengthToCopy = buffer.getLength() < len ? buffer.getLength() : len;
    System.arraycopy(data, buffer.getOffset(), b, off, lengthToCopy);
    buffer.setOffset(buffer.getOffset() + lengthToCopy);
    buffer.setLength(buffer.getLength() - lengthToCopy);

    return lengthToCopy;
  }
  private void fillBuffer() {
    if (buffer == null) {
      buffer = new Buffer();
      buffer.setFormat(track.getFormat());
    }

    do {
      if (buffer.isEOM()) return;

      if (buffer.getLength() > 0) return; // still have data in buffer
      // TODO: any fields to set?

      track.readFrame(buffer);
      logger.fine("Read buffer from track: " + buffer.getLength());

    } while (buffer.isDiscard());
  }