Skip to content

Commit 9a3301d

Browse files
authored
Add files via upload
1 parent c478194 commit 9a3301d

19 files changed

+1864
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package net.sharksystem.asp3;
2+
3+
import java.io.IOException;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
/**
8+
* A chunk represent a set of messages which are issued to a
9+
* topic (described by an UIR).
10+
*
11+
* @author thsc
12+
*/
13+
interface ASP3Chunk {
14+
15+
/**
16+
*
17+
* @return number of message in that chunk
18+
*/
19+
int getNumberMessage();
20+
21+
/**
22+
*
23+
* @return recipients of that chunk
24+
*/
25+
List<CharSequence> getRecipients();
26+
27+
/**
28+
* add recipients
29+
* @param recipient
30+
* @throws IOException
31+
*/
32+
void addRecipient(CharSequence recipient) throws IOException;
33+
34+
/**
35+
* set a list of recipients. Former recipients are dikscarded
36+
* @param recipients
37+
* @throws IOException
38+
*/
39+
void setRecipients(List<CharSequence> recipients) throws IOException;
40+
41+
/**
42+
* recipient is removed
43+
* @param recipients
44+
* @throws IOException
45+
*/
46+
void removeRecipient(CharSequence recipients) throws IOException;
47+
48+
/**
49+
* URI which represents topic of messages in that chunk
50+
* @return
51+
* @throws IOException
52+
*/
53+
String getUri() throws IOException;
54+
55+
/**
56+
* adds a message
57+
* @param message
58+
* @throws IOException
59+
*/
60+
void add(CharSequence message) throws IOException;
61+
62+
/**
63+
*
64+
* @return iterator of all messages in the chunk
65+
* @throws IOException
66+
*/
67+
Iterator<CharSequence> getMessages() throws IOException;
68+
69+
/**
70+
* remove that chunk.. drop all object references after
71+
* calling this methods. Further calls on this object
72+
* have an undefined behaviour.
73+
*/
74+
public void drop();
75+
}
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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

Comments
 (0)