|
| 1 | +package net.sharksystem.asp3; |
| 2 | + |
| 3 | +import java.io.DataInputStream; |
| 4 | +import java.io.DataOutputStream; |
| 5 | +import java.io.EOFException; |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileNotFoundException; |
| 9 | +import java.io.FileOutputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Iterator; |
| 13 | +import java.util.List; |
| 14 | +import java.util.NoSuchElementException; |
| 15 | +import java.util.StringTokenizer; |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | + * @author thsc |
| 20 | + */ |
| 21 | +public class ASP3ChunkFS implements ASP3Chunk { |
| 22 | + public static final String META_DATA_EXTENSION = "meta"; |
| 23 | + public static final String DATA_EXTENSION = "content"; |
| 24 | + public static final String DEFAULT_URL = "content://sharksystem.net/noContext"; |
| 25 | + private final ASP3StorageFS storage; |
| 26 | + private String uri = DEFAULT_URL; |
| 27 | + private ArrayList<CharSequence> recipients; |
| 28 | + private File metaFile; |
| 29 | + private File messageFile; |
| 30 | + |
| 31 | + private int numberMessage = 0; |
| 32 | + |
| 33 | + |
| 34 | + ASP3ChunkFS(ASP3StorageFS storage, String targetUri, int era) throws IOException { |
| 35 | + this.storage = storage; |
| 36 | + this.uri = targetUri; |
| 37 | + |
| 38 | + String trunkName = this.storage.getFullFileName(era, targetUri); |
| 39 | + |
| 40 | + // init |
| 41 | + this.initFiles(trunkName); |
| 42 | + } |
| 43 | + |
| 44 | + ASP3ChunkFS(ASP3StorageFS storage, String trunkName) throws IOException { |
| 45 | + this.storage = storage; |
| 46 | + this.uri = ASP3ChunkFS.DEFAULT_URL; |
| 47 | + |
| 48 | + this.initFiles(trunkName); |
| 49 | + } |
| 50 | + |
| 51 | + private void initFiles(String trunkName) throws IOException { |
| 52 | + String messageFileName = trunkName + "." + DATA_EXTENSION; |
| 53 | + String metaFileName = trunkName + "." + META_DATA_EXTENSION; |
| 54 | + |
| 55 | + this.messageFile = new File(messageFileName); |
| 56 | + this.metaFile = new File(metaFileName); |
| 57 | + |
| 58 | + // init meta file - message file keeps untouched (good idea?) |
| 59 | + if(!this.metaFile.exists()) { |
| 60 | + this.metaFile.createNewFile(); |
| 61 | + } |
| 62 | + |
| 63 | + if(!this.readMetaData(this.metaFile)) { |
| 64 | + // no metadate to be read - set |
| 65 | + this.writeMetaData(this.metaFile); |
| 66 | + this.recipients = new ArrayList<>(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public List<CharSequence> getRecipients() { |
| 72 | + return this.recipients; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void addRecipient(CharSequence recipient) throws IOException { |
| 77 | + this.recipients.add(recipient); |
| 78 | + |
| 79 | + this.writeMetaData(this.metaFile); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void setRecipients(List<CharSequence> newRecipients) throws IOException { |
| 84 | + this.recipients = new ArrayList<>(); |
| 85 | + for(CharSequence recipient : newRecipients) { |
| 86 | + this.recipients.add(recipient); |
| 87 | + } |
| 88 | + |
| 89 | + this.writeMetaData(this.metaFile); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void removeRecipient(CharSequence recipient) throws IOException { |
| 94 | + this.recipients.remove(recipient); |
| 95 | + |
| 96 | + this.writeMetaData(this.metaFile); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String getUri() { |
| 101 | + return (String) this.uri; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void add(CharSequence message) throws IOException { |
| 106 | + DataOutputStream dos; |
| 107 | + dos = new DataOutputStream(new FileOutputStream(this.messageFile, true)); |
| 108 | + |
| 109 | + dos.writeUTF((String) message); |
| 110 | + this.numberMessage++; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Iterator<CharSequence> getMessages() throws IOException { |
| 115 | + try { |
| 116 | + return new MessageIter(this.messageFile); |
| 117 | + } catch (FileNotFoundException ex) { |
| 118 | + throw new IOException(ex.getLocalizedMessage()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void drop() { |
| 124 | + this.metaFile.delete(); |
| 125 | + this.messageFile.delete(); |
| 126 | + } |
| 127 | + |
| 128 | + private static final String RECIPIENTS_LIST_DELIMITER = "|||"; |
| 129 | + |
| 130 | + private boolean readMetaData(File metaFile) throws IOException { |
| 131 | + if(!metaFile.exists()) return false; |
| 132 | + // read data from metafile |
| 133 | + DataInputStream dis = new DataInputStream(new FileInputStream(metaFile)); |
| 134 | + |
| 135 | + try { |
| 136 | + this.uri = dis.readUTF(); |
| 137 | + this.numberMessage = dis.readInt(); |
| 138 | + } |
| 139 | + catch(EOFException eof) { |
| 140 | + // file empty |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + this.recipients = new ArrayList<CharSequence>(); |
| 145 | + |
| 146 | + try { |
| 147 | + String recipientsList = dis.readUTF(); |
| 148 | + |
| 149 | + StringTokenizer t = new StringTokenizer(recipientsList, |
| 150 | + RECIPIENTS_LIST_DELIMITER); |
| 151 | + |
| 152 | + while(t.hasMoreTokens()) { |
| 153 | + this.recipients.add(t.nextToken()); |
| 154 | + } |
| 155 | + } |
| 156 | + catch(IOException ioe) { |
| 157 | + // no more data - ok |
| 158 | + } |
| 159 | + finally { |
| 160 | + dis.close(); |
| 161 | + } |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + private void writeMetaData(File metaFile) throws IOException { |
| 167 | + // read data from metafile |
| 168 | + DataOutputStream dos = new DataOutputStream(new FileOutputStream(metaFile)); |
| 169 | + |
| 170 | + dos.writeUTF(this.uri); |
| 171 | + dos.writeInt(this.numberMessage); |
| 172 | + StringBuilder b = new StringBuilder(); |
| 173 | + |
| 174 | + boolean first = true; |
| 175 | + if(this.recipients != null && !this.recipients.isEmpty()) { |
| 176 | + for(CharSequence recipient : this.recipients) { |
| 177 | + if(recipient != null) { |
| 178 | + if(!first) { |
| 179 | + b.append(RECIPIENTS_LIST_DELIMITER); |
| 180 | + } else { |
| 181 | + first = false; |
| 182 | + } |
| 183 | + b.append(recipient); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + dos.writeUTF(b.toString()); |
| 188 | + |
| 189 | + dos.close(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public int getNumberMessage() { |
| 194 | + return this.numberMessage; |
| 195 | + } |
| 196 | + |
| 197 | + private class MessageIter implements Iterator { |
| 198 | + |
| 199 | + private final DataInputStream dis; |
| 200 | + private String nextString; |
| 201 | + |
| 202 | + |
| 203 | + MessageIter(File messageFile) throws FileNotFoundException { |
| 204 | + this.dis = new DataInputStream(new FileInputStream(messageFile)); |
| 205 | + |
| 206 | + this.lookahead(); |
| 207 | + } |
| 208 | + |
| 209 | + private void lookahead() { |
| 210 | + try { |
| 211 | + this.nextString = this.dis.readUTF(); |
| 212 | + } catch (IOException ex) { |
| 213 | + // empty |
| 214 | + this.nextString = null; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public boolean hasNext() { |
| 220 | + return this.nextString != null; |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public String next() { |
| 225 | + if(this.nextString != null) { |
| 226 | + String s = this.nextString; |
| 227 | + this.lookahead(); |
| 228 | + return s; |
| 229 | + } |
| 230 | + |
| 231 | + throw new NoSuchElementException("no more messages"); |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments