/** * Constructor for deserialization. * @param planetModel is the planet model. * @param inputStream is the input stream. */ public GeoS2Shape(final PlanetModel planetModel, final InputStream inputStream) throws IOException { this(planetModel, (GeoPoint) SerializableObject.readObject(inputStream), (GeoPoint) SerializableObject.readObject(inputStream), (GeoPoint) SerializableObject.readObject(inputStream), (GeoPoint) SerializableObject.readObject(inputStream)); }
/** Write an array. * @param outputStream is the output stream,. * @param values is the array. */ static void writeHeterogeneousArray(final OutputStream outputStream, final SerializableObject[] values) throws IOException { if (values == null) { writeInt(outputStream, 0); } else { writeInt(outputStream, values.length); for (final SerializableObject value : values) { writeObject(outputStream, value); } } }
/** * Constructor for deserialization. * @param planetModel is the planet model. * @param inputStream is the input stream. */ public GeoSouthLatitudeZone(final PlanetModel planetModel, final InputStream inputStream) throws IOException { this(planetModel, SerializableObject.readDouble(inputStream)); }
/** * Constructor for deserialization. * @param planetModel is the planet model. * @param inputStream is the input stream. */ public GeoStandardPath(final PlanetModel planetModel, final InputStream inputStream) throws IOException { this(planetModel, SerializableObject.readDouble(inputStream), SerializableObject.readPointArray(planetModel, inputStream)); }
/** Read an object from a stream (for objects that do not need a PlanetModel). * @param inputStream is the input stream. * @return the deserialized object. */ public static SerializableObject readObject(final InputStream inputStream) throws IOException { try { // read the class final Class<?> clazz = readClass(inputStream); return readObject(inputStream, clazz); } catch (ClassNotFoundException e) { throw new IOException("Can't find class for deserialization: "+e.getMessage(), e); } }
/** Read an array. * @param planetModel is the planet model. * @param inputStream is the input stream. * @param clazz is the class of the objects to read. * @return the array. */ static <T extends SerializableObject> T[] readHomogeneousArray(final PlanetModel planetModel, final InputStream inputStream, final Class<T> clazz) throws IOException { final int count = readInt(inputStream); @SuppressWarnings("unchecked") final T[] rval = (T[])Array.newInstance(clazz, count); for (int i = 0; i < count; i++) { rval[i] = clazz.cast(readObject(planetModel, inputStream, clazz)); } return rval; }
/** * Constructor for deserialization. * @param planetModel is the planet model. * @param inputStream is the input stream. */ public GeoConcavePolygon(final PlanetModel planetModel, final InputStream inputStream) throws IOException { super(planetModel); this.points = java.util.Arrays.asList(SerializableObject.readPointArray(planetModel, inputStream)); final List<GeoPolygon> holes = java.util.Arrays.asList(SerializableObject.readPolygonArray(planetModel, inputStream)); if (holes != null && holes.size() == 0) { this.holes = null; } else { this.holes = holes; } this.isInternalEdges = SerializableObject.readBitSet(inputStream); done(this.isInternalEdges.get(points.size()-1)); }
@Override public void write(final OutputStream outputStream) throws IOException { SerializableObject.writeObject(outputStream, point1); SerializableObject.writeObject(outputStream, point2); SerializableObject.writeObject(outputStream, point3); SerializableObject.writeObject(outputStream, point4); }
private static List<List<GeoPoint>> readPointsList(final PlanetModel planetModel, final InputStream inputStream) throws IOException { final int count = SerializableObject.readInt(inputStream); final List<List<GeoPoint>> array = new ArrayList<>(count); for (int i = 0; i < count; i++) { array.add(java.util.Arrays.asList(SerializableObject.readPointArray(planetModel, inputStream))); } return array; }
/** * Read the class from the stream * @param inputStream is the stream to read from. * @return is the class read */ static Class<?> readClass(final InputStream inputStream) throws IOException, ClassNotFoundException { boolean standard = readBoolean(inputStream); if (standard) { int index = inputStream.read(); return StandardObjects.codeRegsitry.get(index); } else { String className = readString(inputStream); return Class.forName(className); } }
/** Read a long from a stream. * @param inputStream is the input stream. * @return the long value read from the stream. */ static long readLong(final InputStream inputStream) throws IOException { final long lower = ((long)(readInt(inputStream))) & 0x00000000ffffffffL; final long upper = (((long)(readInt(inputStream))) << 32) & 0xffffffff00000000L; return lower + upper; }
/** Read a string from a stream. * @param inputStream is the stream to read from. * @return the string that was read. */ static String readString(final InputStream inputStream) throws IOException { return new String(readByteArray(inputStream), StandardCharsets.UTF_8); }
/** Read a polygon array. * @param planetModel is the planet model. * @param inputStream is the input stream. * @return the array of polygons that was read. */ static GeoPolygon[] readPolygonArray(final PlanetModel planetModel, final InputStream inputStream) throws IOException { return readHeterogeneousArray(planetModel, inputStream, GeoPolygon.class); }
/** Read a point array. * @param planetModel is the planet model. * @param inputStream is the input stream. * @return the array of points that was read. */ static GeoPoint[] readPointArray(final PlanetModel planetModel, final InputStream inputStream) throws IOException { return readHomogeneousArray(planetModel, inputStream, GeoPoint.class); }
/** * Constructor for deserialization. * @param planetModel is the planet model. * @param inputStream is the input stream. */ public GeoComplexPolygon(final PlanetModel planetModel, final InputStream inputStream) throws IOException { this(planetModel, readPointsList(planetModel, inputStream), new GeoPoint(planetModel, inputStream), SerializableObject.readBoolean(inputStream)); }
/** Read an object from a stream (for objects that need a PlanetModel). * @param planetModel is the planet model to use to deserialize the object. * @param inputStream is the input stream. * @return the deserialized object. */ public static SerializableObject readObject(final PlanetModel planetModel, final InputStream inputStream) throws IOException { try { // Read the class final Class<?> clazz = readClass(inputStream); return readObject(planetModel, inputStream, clazz); } catch (ClassNotFoundException e) { throw new IOException("Can't find class for deserialization: "+e.getMessage(), e); } }
/** Read an array. * @param planetModel is the planet model. * @param inputStream is the input stream. * @return the array. */ static <T extends SerializableObject> T[] readHeterogeneousArray(final PlanetModel planetModel, final InputStream inputStream, final Class<T> clazz) throws IOException { final int count = readInt(inputStream); @SuppressWarnings("unchecked") final T[] rval = (T[])Array.newInstance(clazz, count); for (int i = 0; i < count; i++) { rval[i] = clazz.cast(readObject(planetModel, inputStream)); } return rval; }
/** * Constructor for deserialization. * @param planetModel is the planet model. * @param inputStream is the input stream. */ public GeoConvexPolygon(final PlanetModel planetModel, final InputStream inputStream) throws IOException { super(planetModel); this.points = java.util.Arrays.asList(SerializableObject.readPointArray(planetModel, inputStream)); final List<GeoPolygon> holes = java.util.Arrays.asList(SerializableObject.readPolygonArray(planetModel, inputStream)); if (holes != null && holes.size() == 0) { this.holes = null; } else { this.holes = holes; } this.isInternalEdges = SerializableObject.readBitSet(inputStream); done(this.isInternalEdges.get(points.size()-1)); }
@Override public void writeShape(DataOutput dataOutput, Shape s) throws IOException { if (s instanceof Geo3dShape) { Geo3dShape geoAreaShape = (Geo3dShape) s; SerializableObject.writeObject((OutputStream) dataOutput, geoAreaShape.shape); } else { throw new IllegalArgumentException("trying to write a not supported shape: " + s.getClass().getName()); } }
/** Read byte array. * @param inputStream is the input stream. * @return the byte array. */ static byte[] readByteArray(final InputStream inputStream) throws IOException { int stringLength = readInt(inputStream); int stringOffset = 0; final byte[] bytes = new byte[stringLength]; while (stringLength > 0) { final int amt = inputStream.read(bytes, stringOffset, stringLength); if (amt == -1) { throw new IOException("Unexpected end of input stream"); } stringOffset += amt; stringLength -= amt; } return bytes; }