Fix exceptions for readers, add path writer.

This commit is contained in:
Holt59 2018-02-24 15:11:15 +01:00
parent 164a9d4494
commit 6c3bc23984
7 changed files with 194 additions and 96 deletions

View File

@ -1,5 +1,7 @@
package org.insa.graph.io;
import java.io.IOException;
import org.insa.graph.Graph;
public interface AbstractGraphReader {
@ -11,6 +13,6 @@ public interface AbstractGraphReader {
* @throws Exception
*
*/
public Graph read() throws Exception;
public Graph read() throws IOException;
}

View File

@ -1,5 +1,7 @@
package org.insa.graph.io;
import java.io.IOException;
import org.insa.graph.Graph;
import org.insa.graph.Path;
@ -13,6 +15,6 @@ public interface AbstractPathReader {
* @return A new path.
* @throws Exception
*/
public Path readPath(Graph graph) throws Exception;
public Path readPath(Graph graph) throws IOException;
}

View File

@ -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;
}

View File

@ -12,15 +12,15 @@ 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;
protected static final int VERSION = 1;
protected static final int MAGIC_NUMBER = 0xdecafe;
public BinaryPathReader(DataInputStream dis) {
super(MAGIC_NUMBER, VERSION, dis);
}
@Override
public Path readPath(Graph graph) throws Exception {
public Path readPath(Graph graph) throws IOException {
// Read and check magic number and version.
checkMagicNumberOrThrow(dis.readInt());

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -1,6 +1,8 @@
package org.insa.graph.io;
public class MapMismatchException extends Exception {
import java.io.IOException;
public class MapMismatchException extends IOException {
/**
*