diff --git a/src/main/org/insa/graph/io/AbstractGraphReader.java b/src/main/org/insa/graph/io/AbstractGraphReader.java index 5fc4b6f..a1067f6 100644 --- a/src/main/org/insa/graph/io/AbstractGraphReader.java +++ b/src/main/org/insa/graph/io/AbstractGraphReader.java @@ -1,16 +1,18 @@ package org.insa.graph.io; +import java.io.IOException; + import org.insa.graph.Graph; public interface AbstractGraphReader { - - /** - * Read a graph an returns it. - * - * @return Graph. - * @throws Exception - * - */ - public Graph read() throws Exception; + + /** + * Read a graph an returns it. + * + * @return Graph. + * @throws Exception + * + */ + public Graph read() throws IOException; } diff --git a/src/main/org/insa/graph/io/AbstractPathReader.java b/src/main/org/insa/graph/io/AbstractPathReader.java index 296e5d4..53feab3 100644 --- a/src/main/org/insa/graph/io/AbstractPathReader.java +++ b/src/main/org/insa/graph/io/AbstractPathReader.java @@ -1,18 +1,20 @@ package org.insa.graph.io; +import java.io.IOException; + import org.insa.graph.Graph; import org.insa.graph.Path; public interface AbstractPathReader { - /** - * Read a path of the given graph and returns it. - * - * @param graph Graph of the path. - * - * @return A new path. - * @throws Exception - */ - public Path readPath(Graph graph) throws Exception; - + /** + * Read a path of the given graph and returns it. + * + * @param graph Graph of the path. + * + * @return A new path. + * @throws Exception + */ + public Path readPath(Graph graph) throws IOException; + } diff --git a/src/main/org/insa/graph/io/AbstractPathWriter.java b/src/main/org/insa/graph/io/AbstractPathWriter.java new file mode 100644 index 0000000..0e27037 --- /dev/null +++ b/src/main/org/insa/graph/io/AbstractPathWriter.java @@ -0,0 +1,18 @@ +package org.insa.graph.io; + +import java.io.IOException; + +import org.insa.graph.Path; + +public interface AbstractPathWriter { + + /** + * Write a path. + * + * @param path Path to write. + * + * @throws Exception + */ + public void writePath(Path path) throws IOException; + +} diff --git a/src/main/org/insa/graph/io/BinaryPathReader.java b/src/main/org/insa/graph/io/BinaryPathReader.java index 6f2eeeb..53ae922 100644 --- a/src/main/org/insa/graph/io/BinaryPathReader.java +++ b/src/main/org/insa/graph/io/BinaryPathReader.java @@ -11,55 +11,55 @@ import org.insa.graph.Path.CreationMode; public class BinaryPathReader extends BinaryReader implements AbstractPathReader { - // Map version and magic number targeted for this reader. - private static final int VERSION = 1; - private static final int MAGIC_NUMBER = 0xdecafe; + // Map version and magic number targeted for this reader. + protected static final int VERSION = 1; + protected static final int MAGIC_NUMBER = 0xdecafe; - public BinaryPathReader(DataInputStream dis) { - super(MAGIC_NUMBER, VERSION, dis); - } + public BinaryPathReader(DataInputStream dis) { + super(MAGIC_NUMBER, VERSION, dis); + } - @Override - public Path readPath(Graph graph) throws Exception { - - // Read and check magic number and version. - checkMagicNumberOrThrow(dis.readInt()); - checkVersionOrThrow(dis.readInt()); - - // Read map ID and check against graph. - int mapId = dis.readInt(); - - if (mapId != graph.getMapId()) { - throw new MapMismatchException(mapId, graph.getMapId()); - } - - // Number of nodes in the path (without first and last). - int nbNodes = dis.readInt(); - - // Skip (duplicate) first and last node - readNode(graph); - readNode(graph); - - // Read intermediate nodes: - ArrayList nodes = new ArrayList(); - for (int i = 0; i < nbNodes; ++i) { - nodes.add(readNode(graph)); - } - - return new Path(graph, nodes, CreationMode.SHORTEST_TIME); - } + @Override + public Path readPath(Graph graph) throws IOException { - /** - * Read a node from the stream and returns id. - * - * @return - * @throws IOException - */ - protected Node readNode(Graph graph) throws IOException { - // Discard zone. - dis.readUnsignedByte(); - - return graph.getNodes().get(read24bits()); - } + // Read and check magic number and version. + checkMagicNumberOrThrow(dis.readInt()); + checkVersionOrThrow(dis.readInt()); + + // Read map ID and check against graph. + int mapId = dis.readInt(); + + if (mapId != graph.getMapId()) { + throw new MapMismatchException(mapId, graph.getMapId()); + } + + // Number of nodes in the path (without first and last). + int nbNodes = dis.readInt(); + + // Skip (duplicate) first and last node + readNode(graph); + readNode(graph); + + // Read intermediate nodes: + ArrayList nodes = new ArrayList(); + for (int i = 0; i < nbNodes; ++i) { + nodes.add(readNode(graph)); + } + + return new Path(graph, nodes, CreationMode.SHORTEST_TIME); + } + + /** + * Read a node from the stream and returns id. + * + * @return + * @throws IOException + */ + protected Node readNode(Graph graph) throws IOException { + // Discard zone. + dis.readUnsignedByte(); + + return graph.getNodes().get(read24bits()); + } } diff --git a/src/main/org/insa/graph/io/BinaryPathWriter.java b/src/main/org/insa/graph/io/BinaryPathWriter.java new file mode 100644 index 0000000..b8cce09 --- /dev/null +++ b/src/main/org/insa/graph/io/BinaryPathWriter.java @@ -0,0 +1,44 @@ +package org.insa.graph.io; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.insa.graph.Arc; +import org.insa.graph.Path; + +public class BinaryPathWriter extends BinaryWriter implements AbstractPathWriter { + + /** + * @param dos + */ + protected BinaryPathWriter(DataOutputStream dos) { + super(dos); + } + + @Override + public void writePath(Path path) throws IOException { + + // Write magic number and version. + dos.writeInt(BinaryPathReader.MAGIC_NUMBER); + dos.writeInt(BinaryPathReader.VERSION); + + // Write map id. + dos.writeInt(path.getGraph().getMapId()); + + // Write number of racs + dos.writeInt(path.getArcs().size() + 1); + + // Write origin / destination. + dos.writeInt(path.getOrigin().getId()); + dos.writeInt(path.getDestination().getId()); + + // Write nodes. + dos.writeInt(path.getOrigin().getId()); + for (Arc arc: path.getArcs()) { + dos.writeInt(arc.getDestination().getId()); + } + + dos.flush(); + } + +} diff --git a/src/main/org/insa/graph/io/BinaryWriter.java b/src/main/org/insa/graph/io/BinaryWriter.java new file mode 100644 index 0000000..43e5cf7 --- /dev/null +++ b/src/main/org/insa/graph/io/BinaryWriter.java @@ -0,0 +1,30 @@ +package org.insa.graph.io; + +import java.io.DataOutputStream; +import java.io.IOException; + +public abstract class BinaryWriter { + + // Output stream. + protected DataOutputStream dos; + + /** + * @param dos + */ + protected BinaryWriter(DataOutputStream dos) { + this.dos = dos; + } + + /** + * Write a 24-bits integer in BigEndian to the output stream. + * + * @param value + * + * @throws IOException + */ + protected void write24bits(int value) throws IOException { + dos.writeShort(value >> 8); + dos.writeByte(value & 0xff); + } + +} diff --git a/src/main/org/insa/graph/io/MapMismatchException.java b/src/main/org/insa/graph/io/MapMismatchException.java index 10b0fd2..79be472 100644 --- a/src/main/org/insa/graph/io/MapMismatchException.java +++ b/src/main/org/insa/graph/io/MapMismatchException.java @@ -1,36 +1,38 @@ package org.insa.graph.io; -public class MapMismatchException extends Exception { +import java.io.IOException; - /** - * - */ - private static final long serialVersionUID = 3076730078387819138L; - // Actual and expected magic numbers. - private int actualMapId, expectedMapId; - - /** - * - * @param actualVersion - * @param expectedVersion - */ - public MapMismatchException(int actualMapId, int expectedMapId) { - super(); - this.actualMapId = actualMapId; - this.expectedMapId = expectedMapId; - } +public class MapMismatchException extends IOException { - /** - * @return - */ - public int getActualMapId() { - return actualMapId; - } + /** + * + */ + private static final long serialVersionUID = 3076730078387819138L; + // Actual and expected magic numbers. + private int actualMapId, expectedMapId; - /** - * @return - */ - public int getExpectedMapId() { - return expectedMapId; - } + /** + * + * @param actualVersion + * @param expectedVersion + */ + public MapMismatchException(int actualMapId, int expectedMapId) { + super(); + this.actualMapId = actualMapId; + this.expectedMapId = expectedMapId; + } + + /** + * @return + */ + public int getActualMapId() { + return actualMapId; + } + + /** + * @return + */ + public int getExpectedMapId() { + return expectedMapId; + } }