static void insertString(String str, ByteBuffer buf) throws java.io.UnsupportedEncodingException { if (str != null && str.length() > 0) { buf.putInt(str.length() + 1); buf.put(str.getBytes("UTF-8"), 0, str.length()); buf.put((byte) 0); } else { // buffer is null or length 0 buf.putInt(0); } }
static void insertByteString(byte[] array, ByteBuffer buf) throws java.io.UnsupportedEncodingException { if (array != null && array.length > 0) { buf.putInt(array.length); buf.put(array, 0, array.length); buf.put((byte) 0); } else { // buffer is null or length 0 buf.putInt(0); } }
// creates the header and pads the content returning a new byte[] // in the proper format. The secret is 4bytes (int), the step // is 2 bytes (short). public static byte[] createBuffer(byte[] content, int secret, short step) { short studentNum = 456; // my student number is 1340456 int paddedSize = content.length; while (paddedSize % 4 != 0) { paddedSize++; } ByteBuffer bb = ByteBuffer.allocate(HEADER_SIZE + paddedSize); bb.putInt(content.length); bb.putInt(secret); bb.putShort(step); bb.putShort(studentNum); bb.put(content); return bb.array(); }
public final byte[] serialize() { ByteBuffer bb = null; try { final byte[] targetPosition = (this.m_targetPosition == null) ? new byte[0] : this.m_targetPosition.getBytes("UTF-8"); final byte[] jaugeVarName = (this.m_jaugeVarName == null) ? new byte[0] : this.m_jaugeVarName.getBytes("UTF-8"); bb = ByteBuffer.allocate(9 + targetPosition.length + 4 + 1 + 4 + jaugeVarName.length); bb.putInt(this.m_id); bb.put((byte) (this.m_isChallengeGoal ? 1 : 0)); bb.putInt(targetPosition.length); bb.put(targetPosition); bb.put((byte) (this.m_isCountDownJauge ? 1 : 0)); bb.putInt(this.m_jaugeMaxValue); bb.putInt(jaugeVarName.length); bb.put(jaugeVarName); } catch (UnsupportedEncodingException e) { ScenarioBinaryStorable.m_logger.error((Object) "Exception", (Throwable) e); } return bb.array(); }
public final byte[] serialize() { final byte[] cdata = StringUtils.toUTF8(this.m_criterion); final int presize = cdata.length; final ByteBuffer bb = ByteBuffer.allocate(32 + presize); bb.putInt(this.m_id); bb.put(this.m_order); bb.putInt(this.m_gfx); bb.putInt(cdata.length); bb.put(cdata); bb.put((byte) (this.m_success ? 1 : 0)); bb.putInt(this.m_itemId); bb.putShort(this.m_itemQty); bb.putInt(this.m_xp); bb.putInt(this.m_kama); bb.putInt(this.m_guildPoints); return bb.array(); }
@Override public boolean serialize(final ByteBuffer buffer) { buffer.putInt(this.referenceId); buffer.putInt(this.price); return true; }
@Override public boolean serialize(final ByteBuffer buffer) { buffer.putInt(this.referenceId); buffer.putInt(this.remainingDuration); return true; }
private void setRingCursor(ByteBuffer buffer, int newCursor) { checkArgument(newCursor >= 0); buffer.putInt(4, newCursor); }
/** * Opens new WebRTC data channel using specified parameters. * * @param type channel type as defined in control protocol description. Use 0 for "reliable". * @param prio channel priority. The higher the number, the lower the priority. * @param reliab Reliability Parameter<br> * This field is ignored if a reliable channel is used. If a partial reliable channel with * limited number of retransmissions is used, this field specifies the number of * retransmissions. If a partial reliable channel with limited lifetime is used, this field * specifies the maximum lifetime in milliseconds. The following table summarizes this:<br> * </br> * <p>+------------------------------------------------+------------------+ | Channel Type | * Reliability | | | Parameter | * +------------------------------------------------+------------------+ | * DATA_CHANNEL_RELIABLE | Ignored | | DATA_CHANNEL_RELIABLE_UNORDERED | Ignored | | * DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT | Number of RTX | | * DATA_CHANNEL_PARTIAL_RELIABLE_REXMIT_UNORDERED | Number of RTX | | * DATA_CHANNEL_PARTIAL_RELIABLE_TIMED | Lifetime in ms | | * DATA_CHANNEL_PARTIAL_RELIABLE_TIMED_UNORDERED | Lifetime in ms | * +------------------------------------------------+------------------+ * @param sid SCTP stream id that will be used by new channel (it must not be already used). * @param label text label for the channel. * @return new instance of <tt>WebRtcDataStream</tt> that represents opened WebRTC data channel. * @throws IOException if IO error occurs. */ public synchronized WebRtcDataStream openChannel( int type, int prio, long reliab, int sid, String label) throws IOException { if (channels.containsKey(sid)) { throw new IOException("Channel on sid: " + sid + " already exists"); } // Label Length & Label byte[] labelBytes; int labelByteLength; if (label == null) { labelBytes = null; labelByteLength = 0; } else { labelBytes = label.getBytes("UTF-8"); labelByteLength = labelBytes.length; if (labelByteLength > 0xFFFF) labelByteLength = 0xFFFF; } // Protocol Length & Protocol String protocol = WEBRTC_DATA_CHANNEL_PROTOCOL; byte[] protocolBytes; int protocolByteLength; if (protocol == null) { protocolBytes = null; protocolByteLength = 0; } else { protocolBytes = protocol.getBytes("UTF-8"); protocolByteLength = protocolBytes.length; if (protocolByteLength > 0xFFFF) protocolByteLength = 0xFFFF; } ByteBuffer packet = ByteBuffer.allocate(12 + labelByteLength + protocolByteLength); // Message open new channel on current sid // Message Type packet.put((byte) MSG_OPEN_CHANNEL); // Channel Type packet.put((byte) type); // Priority packet.putShort((short) prio); // Reliability Parameter packet.putInt((int) reliab); // Label Length packet.putShort((short) labelByteLength); // Protocol Length packet.putShort((short) protocolByteLength); // Label if (labelByteLength != 0) { packet.put(labelBytes, 0, labelByteLength); } // Protocol if (protocolByteLength != 0) { packet.put(protocolBytes, 0, protocolByteLength); } int sentCount = sctpSocket.send(packet.array(), true, sid, WEB_RTC_PPID_CTRL); if (sentCount != packet.capacity()) { throw new IOException("Failed to open new chanel on sid: " + sid); } WebRtcDataStream channel = new WebRtcDataStream(sctpSocket, sid, label, false); channels.put(sid, channel); return channel; }
@Override public byte[] getBinaryData() { final ArrayList<byte[]> binGroups = new ArrayList<byte[]>(this.m_actionGroups.size()); final ArrayList<byte[]> binRewars = new ArrayList<byte[]>(this.m_rewards.size()); int presize = 0; presize += 4; try { for (final String var : this.m_varMapping) { presize += 4 + var.getBytes("UTF-8").length; } } catch (Exception e) { ScenarioBinaryStorable.m_logger.error((Object) "Exception", (Throwable) e); } try { presize += 8; presize += ((this.m_joinCriterion != null) ? this.m_joinCriterion.getBytes("UTF-8").length : 0); presize += ((this.m_rewardEligibilityCriterion != null) ? this.m_rewardEligibilityCriterion.getBytes("UTF-8").length : 0); } catch (Exception e) { ScenarioBinaryStorable.m_logger.error((Object) "Exception", (Throwable) e); } for (final ActionGroupStorable g : this.m_actionGroups) { final byte[] bin = g.serialize(); presize += bin.length + 4; binGroups.add(bin); } for (final RewardStorable r : this.m_rewards) { final byte[] bin = r.serialize(); presize += bin.length + 4; binRewars.add(bin); } try { presize += 4; presize += ((this.m_params != null) ? this.m_params.getBytes("UTF-8").length : 0); } catch (Exception e) { ScenarioBinaryStorable.m_logger.error((Object) "Exception", (Throwable) e); } final ByteBuffer bb = ByteBuffer.allocate(31 + presize); bb.putInt(this.m_id); bb.put(this.m_type); bb.put(this.m_userType); bb.put((byte) (this.m_autoTrigger ? 1 : 0)); bb.put((byte) (this.m_isChallenge ? 1 : 0)); bb.put((byte) (this.m_isChaos ? 1 : 0)); bb.putShort(this.m_duration); bb.putShort(this.m_minUsers); bb.putShort(this.m_maxUsers); if (this.m_expirationDate != null) { bb.putLong(this.m_expirationDate.toLong()); } else { bb.putLong(0L); } try { if (this.m_params != null) { final byte[] bytes = this.m_params.getBytes("UTF-8"); bb.putInt(bytes.length); bb.put(bytes); } else { bb.putInt(0); } } catch (Exception e2) { ScenarioBinaryStorable.m_logger.error((Object) "Exception", (Throwable) e2); } try { bb.putInt(this.m_varMapping.length); for (final String var2 : this.m_varMapping) { final byte[] bytes2 = var2.getBytes("UTF-8"); bb.putInt(bytes2.length); bb.put(bytes2); } } catch (Exception e2) { ScenarioBinaryStorable.m_logger.error((Object) "Exception", (Throwable) e2); } try { if (this.m_joinCriterion != null) { final byte[] bytes = this.m_joinCriterion.getBytes("UTF-8"); bb.putInt(bytes.length); bb.put(bytes); } else { bb.putInt(0); } if (this.m_rewardEligibilityCriterion != null) { final byte[] bytes = this.m_rewardEligibilityCriterion.getBytes("UTF-8"); bb.putInt(bytes.length); bb.put(bytes); } else { bb.putInt(0); } } catch (Exception e2) { ScenarioBinaryStorable.m_logger.error((Object) "Exception", (Throwable) e2); } bb.putInt(binGroups.size()); for (final byte[] gdata : binGroups) { bb.putInt(gdata.length); bb.put(gdata); } bb.putInt(binRewars.size()); for (final byte[] rdata : binRewars) { bb.putInt(rdata.length); bb.put(rdata); } return bb.array(); }
static void insertByteArray(byte[] array, ByteBuffer buf) { if (array != null && array.length > 0) { buf.putInt(array.length); buf.put(array, 0, array.length); } else buf.putInt(0); }
// returns byte array of content meant to be sent in packet b1 public static byte[] partB(int packet_id, int len) { ByteBuffer bb = ByteBuffer.allocate(len + 4); bb.putInt(packet_id); return bb.array(); }