No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinaryPathWriter.java 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package org.insa.graph.io;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import org.insa.graph.Arc;
  6. import org.insa.graph.Path;
  7. /**
  8. * Implementation of {@link PathWriter} to write paths in binary format.
  9. *
  10. */
  11. public class BinaryPathWriter extends BinaryWriter implements PathWriter {
  12. /**
  13. * Create a new BinaryPathWriter that writes to the given output stream.
  14. *
  15. * @param dos Output stream to write to.
  16. */
  17. public BinaryPathWriter(DataOutputStream dos) {
  18. super(dos);
  19. }
  20. @Override
  21. public void writePath(Path path) throws IOException {
  22. // Write magic number and version.
  23. dos.writeInt(BinaryPathReader.MAGIC_NUMBER);
  24. dos.writeInt(BinaryPathReader.VERSION);
  25. // Write map id.
  26. byte[] bytes = Arrays.copyOf(path.getGraph().getMapId().getBytes("UTF-8"),
  27. BinaryGraphReader.MAP_ID_FIELD_LENGTH);
  28. dos.write(bytes);
  29. // Write number of arcs
  30. dos.writeInt(path.getArcs().size() + 1);
  31. // Write origin / destination.
  32. dos.writeInt(path.getOrigin().getId());
  33. dos.writeInt(path.getDestination().getId());
  34. // Write nodes.
  35. dos.writeInt(path.getOrigin().getId());
  36. for (Arc arc: path.getArcs()) {
  37. dos.writeInt(arc.getDestination().getId());
  38. }
  39. dos.flush();
  40. dos.close();
  41. }
  42. }