@@ -0,0 +1,2657 @@
+package com.sbandara.cloudpokes.util;%0A%0Aimport java.nio.ByteBuffer;%0A%0A/**%0A * Utility class for building APNS version 2-style request packets. Packets%0A * begin with a command byte and a four-byte integer indicating the total length%0A * of allvpacket items to follow. Each item is made up of a byte indicating the%0A * type of item, a two-byte integer indicating the length of the item content,%0A * andvthe item itself. All data is written big endian.%0A */%0Apublic class PacketBuilder %7B%0A%09%0A%09private final ByteBuffer buf;%0A%09private byte%5B%5D packet = null;%0A%09%0A%09/**%0A%09 * Constructor for building APNS version 2-style request packets.%0A%09 * @param capacity the maximum possible length of the packet in bytes%0A%09 */%0A%09public PacketBuilder(int capacity) %7B%0A%09%09buf = ByteBuffer.allocate(capacity);%0A%09%09buf.put((byte) 2).mark();%0A%09%09buf.putInt(0);%0A%09%7D%0A%09%0A%09private void ensureIsBuilding() %7B%0A%09%09if (packet != null) %7B%0A%09%09%09throw new IllegalStateException(%22Packet was already built.%22);%0A%09%09%7D%0A%09%7D%0A%09%0A%09/**%0A%09 * Appends an item that consists of an array of bytes to the packet.%0A%09 * @param id an identifier for the type of item%0A%09 * @param item the array-of-bytes content of the item%0A%09 * @return this PacketBuilder instance for fluent use%0A%09 */%0A%09public PacketBuilder putArrayItem(byte id, byte%5B%5D item) %7B%0A%09%09ensureIsBuilding();%0A%09%09buf.put(id).putShort((short) item.length).put(item);%0A%09%09return this;%0A%09%7D%0A%0A%09/**%0A%09 * Appends an item that consists of a four-byte integer to the packet.%0A%09 * @param id an identifier for the type of item%0A%09 * @param item the 4-byte integer content of the item%0A%09 * @return this PacketBuilder instance for fluent use%0A%09 */%0A%09public PacketBuilder putIntItem(byte id, int item) %7B%0A%09%09ensureIsBuilding();%0A%09%09buf.put(id).putShort((short) 4).putInt(item);%0A%09%09return this;%0A%09%7D%0A%0A%09/**%0A%09 * Appends an item that consists of a single byte to the packet.%0A%09 * @param id an identifier for the type of item%0A%09 * @param item the one-byte content of the item%0A%09 * @return this PacketBuilder instance for fluent use%0A%09 */%0A%09public PacketBuilder putByteItem(byte id, byte item) %7B%0A%09%09ensureIsBuilding();%0A%09%09buf.put(id).putShort((short) 1).put(item);%0A%09%09return this;%0A%09%7D%0A%09%0A%09/**%0A%09 * Constructs and returns the packet as an array of bytes. Attempts to%0A%09 * modify the packet through this builder instance after build was called%0A%09 * will raise an %7B@code IllegalStateException%7D.%0A%09 * @return the fully constructed packet%0A%09 * @throws IllegalStateException%0A%09 */%0A%09public byte%5B%5D build() throws IllegalStateException %7B%0A%09%09if (packet == null) %7B%0A%09%09%09final int total_len = buf.position();%0A%09%09%09buf.reset();%0A%09%09%09buf.putInt(total_len - 5);%0A%09%09%09buf.rewind();%0A%09%09%09packet = new byte%5Btotal_len%5D;%0A%09%09%09buf.get(packet, 0, total_len);%0A%09%09%7D%0A%09%09return packet;%0A%09%7D%0A%7D%0A

Add packet builder.