|
| 1 | +# jPack |
| 2 | + |
| 3 | +Writing network code in Java isn't nice. It lacks unsigned types and POJO can't be simple packed & unpacked. |
| 4 | +Fear not! This handy (hopefully) set of classes let's you annotate POJOs to make it simple to read & write network protocols. |
| 5 | + |
| 6 | +# Adding the dependency |
| 7 | + |
| 8 | +We use [Maven](http://maven.apache.org/) for building & distributing jPack. You're welcome to use our Maven repositories, or build your own .jar. |
| 9 | + |
| 10 | +To use our Maven repos just add: |
| 11 | + |
| 12 | + <repositories> |
| 13 | + <repository> |
| 14 | + <id>monits-snapshots</id> |
| 15 | + <url>http://nexus.monits.com/content/repositories/oss-snapshots/</url> |
| 16 | + <name>Monits Snapshots</name> |
| 17 | + </repository> |
| 18 | + </repositories> |
| 19 | + |
| 20 | + <dependencies> |
| 21 | + <dependency> |
| 22 | + <groupId>com.monits</groupId> |
| 23 | + <artifactId>jpack</artifactId> |
| 24 | + <version>1.0-SNAPSHOT</version> |
| 25 | + </dependency> |
| 26 | + </dependencies> |
| 27 | + |
| 28 | +To build a .jar from source: |
| 29 | + |
| 30 | +> |
| 31 | +> mvn clean install |
| 32 | +> |
| 33 | +
|
| 34 | +# Using jPack |
| 35 | + |
| 36 | +The usage is simple, annotate your POJOs and then use `Packer.pack` or `Packer.unpack`. |
| 37 | + |
| 38 | +## `@Encode` |
| 39 | +jPack will only pay attention to fields annotated with `@Encode`, any other fields will be ignored. The important part is that this annotation lets jPack know the order of the fields in the byte stream, via a non-optional integer parameter. jPack expects that a getter and a setter (following the Java Bean convention) exist for a field with `@Encode`. |
| 40 | + |
| 41 | +```java |
| 42 | + |
| 43 | +public class FirstExample { |
| 44 | + @Encode(0) |
| 45 | + private byte first; |
| 46 | + |
| 47 | + @Encode(1) |
| 48 | + private byte second; |
| 49 | + |
| 50 | + public byte getFirst() { |
| 51 | + return first; |
| 52 | + } |
| 53 | + |
| 54 | + public void setFirst(byte first) { |
| 55 | + this.first = first; |
| 56 | + } |
| 57 | + |
| 58 | + public byte getSecond() { |
| 59 | + return second; |
| 60 | + } |
| 61 | + |
| 62 | + public void setSecond(byte second) { |
| 63 | + this.second = second; |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Unsigned integers |
| 70 | + |
| 71 | +Java has no unsigned types. That sucks. So of course jPack gives you a way to use unsigned integers. `@Unsigned` indicates an integer type field is unsigned. This applies to arrays of integer types too. A standard conversion is applied to fit unsigned types in Java's signed types: |
| 72 | + - A short represents an unsigned byte |
| 73 | + - An int represents an unsigned short |
| 74 | + - A long represents an unsigned int |
| 75 | + |
| 76 | +```java |
| 77 | + @Encode(0) |
| 78 | + @Unsigned |
| 79 | + private long anUnsignedInt; |
| 80 | +``` |
| 81 | + |
| 82 | +## Nested types |
| 83 | + |
| 84 | +When jPack finds a type it doesn't know of in a field annotated with `@Encode` it tries to treat that type as another annotated POJO. That means the following will work as expected: |
| 85 | + |
| 86 | +```java |
| 87 | + |
| 88 | +public class NestedObject { |
| 89 | + |
| 90 | + @Encode(0) |
| 91 | + @Unsigned |
| 92 | + private int field; |
| 93 | + |
| 94 | + public int getField() { |
| 95 | + return field; |
| 96 | + } |
| 97 | + |
| 98 | + public void setField(int field) { |
| 99 | + this.field = field; |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +public class BigObject { |
| 105 | + |
| 106 | + @Encode(0) |
| 107 | + private NestedObject obj; |
| 108 | + |
| 109 | + public void setObj(NestedObject obj) { |
| 110 | + this.obj = obj; |
| 111 | + } |
| 112 | + |
| 113 | + public NestedObject getObj() { |
| 114 | + return obj; |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +And when I say "as expected" I mean, NestedObject and BigObject will pack & unpack in the same way. |
| 120 | + |
| 121 | +## Arrays |
| 122 | + |
| 123 | +You can have arrays of any type, as long as jPack knows how to treat that type. You only have to indicate the length of the array in some way. |
| 124 | + |
| 125 | +### Fixed length arrays |
| 126 | + |
| 127 | +With `@FixedLength` you can indicate that an array will always have the same length. Say you have an array of four unsigned integers, you just write: |
| 128 | + |
| 129 | +```java |
| 130 | + |
| 131 | + @Encode(0) |
| 132 | + @FixedLength(4) |
| 133 | + private int[] myInts; |
| 134 | + |
| 135 | + public int[] getMyInts() { |
| 136 | + return myInts; |
| 137 | + } |
| 138 | + |
| 139 | + public void setMyInts() { |
| 140 | + this.myInts = myInts; |
| 141 | + } |
| 142 | + |
| 143 | +``` |
| 144 | + |
| 145 | +### Variable length arrays |
| 146 | + |
| 147 | +Sadly, jPack can't do magic and guess the length of your array. If you have an array of variable length, there must be a another field that indicates it's length. To tell jPack which field it is, just use `@DependsOn` (More on this annotation on the Custom Codec section). |
| 148 | + |
| 149 | +Here we define an array of bytes, that will have it's length determined by a field called `totalBytes` (getters and setters skipped for brevity): |
| 150 | + |
| 151 | +```java |
| 152 | + |
| 153 | + @Encode(0) |
| 154 | + @Unsigned |
| 155 | + private long totalBytes; |
| 156 | + |
| 157 | + @Encode(1) |
| 158 | + @DependsOn({ "totalBytes" }) |
| 159 | + private byte[] lotsaBytes; |
| 160 | + |
| 161 | +``` |
| 162 | + |
| 163 | +## Custom Codec |
| 164 | + |
| 165 | +Sometimes you need a little extra, thus Custom Codecs. Any class that implements the interface `Codec` can be used to pack & unpack fields. By annotating a field with `@UseCodec` you can force jPack to use that codec. |
| 166 | + |
| 167 | +### `@DependsOn` |
| 168 | + |
| 169 | +If your codec needs to know the values of other fields before doing it's magic, you can add `@DependsOn` on the field. jPack will just get those values for you and make them readibly available at coding/decoding time. |
| 170 | + |
| 171 | + |
| 172 | +# Contributing |
| 173 | + |
| 174 | +We encourage you to contribute to this project! |
| 175 | + |
| 176 | +We are also looking forward to your bug reports, feature requests and questions. |
| 177 | + |
| 178 | +# Copyright and License |
| 179 | + |
| 180 | +Copyright 2012 Monits. |
| 181 | + |
| 182 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License at: |
| 183 | + |
| 184 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 185 | + |
0 commit comments