Separated the subject line from the message body.

This commit is contained in:
akwizgran
2011-10-21 18:25:25 +01:00
parent 4d23e14d89
commit f2d80825bc
12 changed files with 95 additions and 54 deletions

View File

@@ -39,37 +39,42 @@ class MessageEncoderImpl implements MessageEncoder {
this.writerFactory = writerFactory;
}
public Message encodeMessage(MessageId parent, byte[] body)
public Message encodeMessage(MessageId parent, String subject, byte[] body)
throws IOException, GeneralSecurityException {
return encodeMessage(parent, null, null, null, null, body);
return encodeMessage(parent, null, null, null, null, subject, body);
}
public Message encodeMessage(MessageId parent, Group group, byte[] body)
throws IOException, GeneralSecurityException {
return encodeMessage(parent, group, null, null, null, body);
public Message encodeMessage(MessageId parent, Group group, String subject,
byte[] body) throws IOException, GeneralSecurityException {
return encodeMessage(parent, group, null, null, null, subject, body);
}
public Message encodeMessage(MessageId parent, Group group,
PrivateKey groupKey, byte[] body) throws IOException,
GeneralSecurityException {
return encodeMessage(parent, group, groupKey, null, null, body);
PrivateKey groupKey, String subject, byte[] body)
throws IOException, GeneralSecurityException {
return encodeMessage(parent, group, groupKey, null, null, subject,
body);
}
public Message encodeMessage(MessageId parent, Group group, Author author,
PrivateKey authorKey, byte[] body) throws IOException,
GeneralSecurityException {
return encodeMessage(parent, group, null, author, authorKey, body);
PrivateKey authorKey, String subject, byte[] body)
throws IOException, GeneralSecurityException {
return encodeMessage(parent, group, null, author, authorKey, subject,
body);
}
public Message encodeMessage(MessageId parent, Group group,
PrivateKey groupKey, Author author, PrivateKey authorKey,
byte[] body) throws IOException, GeneralSecurityException {
String subject, byte[] body) throws IOException,
GeneralSecurityException {
if((author == null) != (authorKey == null))
throw new IllegalArgumentException();
if((group == null || group.getPublicKey() == null) !=
(groupKey == null))
throw new IllegalArgumentException();
if(subject.getBytes("UTF-8").length > Message.MAX_SUBJECT_LENGTH)
throw new IllegalArgumentException();
if(body.length > Message.MAX_BODY_LENGTH)
throw new IllegalArgumentException();
@@ -98,6 +103,7 @@ class MessageEncoderImpl implements MessageEncoder {
else group.writeTo(w);
if(author == null) w.writeNull();
else author.writeTo(w);
w.writeString(subject);
long timestamp = System.currentTimeMillis();
w.writeInt64(timestamp);
byte[] salt = new byte[Message.SALT_LENGTH];
@@ -130,6 +136,7 @@ class MessageEncoderImpl implements MessageEncoder {
MessageId id = new MessageId(messageDigest.digest());
GroupId groupId = group == null ? null : group.getId();
AuthorId authorId = author == null ? null : author.getId();
return new MessageImpl(id, parent, groupId, authorId, timestamp, raw);
return new MessageImpl(id, parent, groupId, authorId, subject,
timestamp, raw);
}
}

View File

@@ -11,15 +11,17 @@ class MessageImpl implements Message {
private final MessageId id, parent;
private final GroupId group;
private final AuthorId author;
private final String subject;
private final long timestamp;
private final byte[] raw;
public MessageImpl(MessageId id, MessageId parent, GroupId group,
AuthorId author, long timestamp, byte[] raw) {
AuthorId author, String subject, long timestamp, byte[] raw) {
this.id = id;
this.parent = parent;
this.group = group;
this.author = author;
this.subject = subject;
this.timestamp = timestamp;
this.raw = raw;
}
@@ -40,6 +42,10 @@ class MessageImpl implements Message {
return author;
}
public String getSubject() {
return subject;
}
public long getTimestamp() {
return timestamp;
}

View File

@@ -76,6 +76,8 @@ class MessageReader implements ObjectReader<Message> {
author = r.readUserDefined(Types.AUTHOR, Author.class);
r.removeObjectReader(Types.AUTHOR);
}
// Read the subject
String subject = r.readString(Message.MAX_SUBJECT_LENGTH);
// Read the timestamp
long timestamp = r.readInt64();
if(timestamp < 0L) throw new FormatException();
@@ -128,6 +130,7 @@ class MessageReader implements ObjectReader<Message> {
MessageId id = new MessageId(messageDigest.digest());
GroupId groupId = group == null ? null : group.getId();
AuthorId authorId = author == null ? null : author.getId();
return new MessageImpl(id, parent, groupId, authorId, timestamp, raw);
return new MessageImpl(id, parent, groupId, authorId, subject,
timestamp, raw);
}
}