All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ucar.nc2.ft.point.remote.RemotePointFeatureIterator Maven / Gradle / Ivy

package ucar.nc2.ft.point.remote;

import opendap.dap.http.HTTPMethod;
import ucar.nc2.stream.NcStream;
import ucar.nc2.stream.NcStreamProto;
import ucar.nc2.ft.PointFeature;
import ucar.nc2.ft.point.PointIteratorAbstract;

import java.io.InputStream;
import java.io.IOException;

/**
 * Iterate through a stream of PointStream.MessageType.PointFeature until PointStream.MessageType.End
 *
 * @author caron
 * @since May 14, 2009
 */
public class RemotePointFeatureIterator extends PointIteratorAbstract {
  private static final boolean debug = false;

  private InputStream in;
  private FeatureMaker featureMaker;

  private PointFeature pf;
  private boolean finished = false;

  RemotePointFeatureIterator(InputStream in, FeatureMaker featureMaker) throws IOException {
    this.in = in;
    this.featureMaker = featureMaker;
  }

  public void finish() {
    if (finished) return;
    if (in != null)
      try { in.close(); } catch (IOException ioe) {}
    in = null;
    finishCalcBounds();
    finished = true;
  }

  public boolean hasNext() throws IOException {
    if (finished) return false;

    PointStream.MessageType mtype = PointStream.readMagic(in);
    if (mtype == PointStream.MessageType.PointFeature) {
      int len = NcStream.readVInt(in);
      if (debug && (getCount() % 100 == 0))
        System.out.println(" RemotePointFeatureIterator len= " + len + " count = " + getCount());

      byte[] b = new byte[len];
      NcStream.readFully(in, b);

      pf = featureMaker.make(b);
      return true;

    } else if (mtype == PointStream.MessageType.End) {
      pf = null;
      finish();
      return false;
      
    } else if (mtype == PointStream.MessageType.Error) {
      int len = NcStream.readVInt(in);
      byte[] b = new byte[len];
      NcStream.readFully(in, b);
      NcStreamProto.Error proto = NcStreamProto.Error.parseFrom(b);
      String errMessage = NcStream.decodeErrorMessage(proto);

      pf = null;
      finish();
      throw new IOException(errMessage);

    } else {
      pf = null;
      finish();
      throw new IOException("Illegal pointstream message type= "+mtype); // maybe kill the socket ?
    }
  }

  public PointFeature next() throws IOException {
    if (null == pf) return null;
    calcBounds(pf);
    return pf;
  }

  public void setBufferSize(int bytes) {
  }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy