This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
forked from nsqio/nsq-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move message encode/decode to utility class
- New MessageCodec class - Also remove all import foo.* in non-test code. Refs Issue nsqio#4
- Loading branch information
Andy O'Neill
committed
Apr 16, 2013
1 parent
44dca4e
commit 966faec
Showing
4 changed files
with
61 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ly.bit.nsq; | ||
|
||
|
||
import ly.bit.nsq.exceptions.NSQException; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
|
||
public class MessageCodec { | ||
public static Message decode(byte[] data, Connection conn) throws NSQException { | ||
DataInputStream ds = new DataInputStream(new ByteArrayInputStream(data)); | ||
try { | ||
long timestamp = ds.readLong(); // 8 bytes | ||
short attempts = ds.readShort(); // 2 bytes | ||
byte[] id = new byte[16]; | ||
ds.read(id); | ||
byte[] body = new byte[data.length - 26]; | ||
ds.read(body); | ||
return new Message(id, body, timestamp, attempts, conn); | ||
} catch (IOException e) { | ||
throw new NSQException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Reverse of decodeMessage, helpful in testing so far. | ||
* @param msg | ||
* @return | ||
* @throws NSQException | ||
*/ | ||
public static byte[] encode(Message msg) throws NSQException { | ||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
DataOutputStream ds = new DataOutputStream(bytes); | ||
try { | ||
ds.writeLong(msg.getTimestamp()); | ||
ds.writeShort(msg.getAttempts()); | ||
ds.write(msg.getId()); | ||
ds.write(msg.getBody()); | ||
ds.close(); | ||
} catch (IOException e) { | ||
throw new NSQException(e); | ||
} | ||
return bytes.toByteArray(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters