Optimizing the reader

The polyline reader presented in the section Developing a new reader is not the best because it generates a large number of temporary objects. What happens is that each time a polyline is read, a list, and hence memory, is allocated to store its points. If the number of polylines in the file is very high, this might cause memory to become fragmented and also the frequent running of the garbage collector, thus impairing performance.
To improve performance, the simple polyline reader can be optimized so that the IlvMapFeatureIterator always returns the same instance of IlvMapFeature. The list for storing the points will be allocated only once--when these points are read for the first time. During subsequent readings, the list will be reallocated only if necessary. To make this possible, the map feature returned by the getNextFeature method is volatile, meaning that its geometry and attributes must be used before the method is called again. All the readers provided in the JViews Maps library that implement the IlvMapFeatureIterator interface work this way.
The complete source code for this optimized reader example can be found in the following file:
The readPolyline method of the class OptimizedPolylineReader resets the points making up the IlvMapLineString geometry to 0. Note that this geometry is now a field of the class.
 geometry.removeAll();
The points of each polyline read are stored in a coordinate buffer. New instances of IlvCoordinate are created only if the polyline being read has more points than each of the polylines previously read.
if (currentPointNum < oldPointNum) {
  // Use an IlvCoordinate that was already allocated.
  c = (IlvCoordinate)points.elementAt(currentPointNum);
} else {
  c = new IlvCoordinate();
  points.addElement(c);
}
Once the x,y coordinates of a polyline point are read, this point is added to the geometry. Geometries are implemented in such a way that the addPoint method does not reallocate memory if one of the previously read geometries had at least as many points as the current geometry that is being read.
  geometry.addPoint(c);
Also, the geometry attributes are stored as fields of the OptimizedPolylineReader class and modified in the readPolyline method.
if (buffer.length() > 0) {
  stringAttribute.setString(buffer.toString());
  attributeProperty.setAttribute(0,stringAttribute);
} else {
  attributeProperty.setAttribute(0,null);
}
feature.setAttributes(attributeProperty);