Advertisement
Guest User

TaggedFile

a guest
Jul 26th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. package resbots.tagfile;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.UUID;
  11.  
  12. import resbots.tagfile.tag.Tag;
  13. import resbots.tagfile.tag.TagByte;
  14. import resbots.tagfile.tag.TagDouble;
  15. import resbots.tagfile.tag.TagInteger;
  16. import resbots.tagfile.tag.TagLong;
  17. import resbots.tagfile.tag.TagShort;
  18.  
  19. public class TaggedFile {
  20.  
  21. private UUID uUID;
  22.  
  23. private File file;
  24.  
  25. private ArrayList<Tag> tags;
  26.  
  27. public TaggedFile(File file) {
  28. this.uUID = UUID.randomUUID();
  29.  
  30. this.file = file;
  31.  
  32. this.tags = new ArrayList<Tag>();
  33. }
  34.  
  35. public void save() throws IOException {
  36. DataOutputStream dOS = new DataOutputStream(new FileOutputStream(file));
  37.  
  38. for (Tag tag : tags) {
  39. dOS.writeByte(tag.getTagID());
  40. tag.save(dOS);
  41. }
  42.  
  43. dOS.writeByte(Tag.TAG_END);
  44.  
  45. dOS.flush();
  46. dOS.close();
  47. }
  48.  
  49. public void load() throws IOException {
  50. DataInputStream dIS = new DataInputStream(new FileInputStream(file));
  51.  
  52. byte tagID = dIS.readByte();
  53.  
  54. while (tagID != Tag.TAG_END) {
  55. Tag newTag = null;
  56.  
  57. switch (tagID) {
  58. case Tag.TAG_BYTE:
  59. newTag = new TagByte((byte) 0);
  60. newTag.load(dIS);
  61.  
  62. tags.add(newTag);
  63. break;
  64. case Tag.TAG_SHORT:
  65. newTag = new TagShort((short) 0);
  66. newTag.load(dIS);
  67.  
  68. tags.add(newTag);
  69. break;
  70. case Tag.TAG_INT:
  71. newTag = new TagInteger(0);
  72. newTag.load(dIS);
  73.  
  74. tags.add(newTag);
  75. break;
  76. case Tag.TAG_LONG:
  77. newTag = new TagLong(0);
  78. newTag.load(dIS);
  79.  
  80. tags.add(newTag);
  81. break;
  82. case Tag.TAG_DOUBLE:
  83. newTag = new TagDouble(0);
  84. newTag.load(dIS);
  85.  
  86. tags.add(newTag);
  87. break;
  88. }
  89.  
  90. tagID = dIS.readByte();
  91. }
  92.  
  93. dIS.close();
  94. }
  95.  
  96. public void addTag(Tag newTag) {
  97. tags.add(newTag);
  98. }
  99.  
  100. public UUID getUUID() {
  101. return uUID;
  102. }
  103.  
  104. public File getFile() {
  105. return file;
  106. }
  107.  
  108. public ArrayList<Tag> getTags() {
  109. return tags;
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement