@Test public void testGetMaxAge() { try { long maxAge = resp.getMaxAge(); assertEquals(60, maxAge); } catch (CoAPException e) { e.printStackTrace(); } int maxAgeValue = 20; byte[] bytes = BitUtil.splitIntToBytes(maxAgeValue); CoAPOptionHeader maxAgeOpt = new CoAPOptionHeader( CoAPOptionName.MAX_AGE, bytes); resp.addOptionHeader(maxAgeOpt); try { long maxAge = resp.getMaxAge(); assertEquals(20, maxAge); } catch (CoAPException e) { e.printStackTrace(); } long unsignedLong = 0xffffffffL & Integer.MAX_VALUE; byte[] longBytes = BitUtil.splitLongToBytes(unsignedLong); byte[] intValue = new byte[]{ longBytes[4], longBytes[5], longBytes[6], longBytes[7] };; resp.removeOptionHeader(maxAgeOpt); maxAgeOpt = new CoAPOptionHeader(CoAPOptionName.MAX_AGE, intValue); resp.addOptionHeader(maxAgeOpt); try { long maxAge = resp.getMaxAge(); assertEquals(Integer.MAX_VALUE, maxAge); } catch (CoAPException e) { e.printStackTrace(); } resp.removeOptionHeader(maxAgeOpt); double max = Math.pow(2, 32) - 1; Double maxDouble = max; long unsignedIntMax = maxDouble.longValue(); longBytes = BitUtil.splitLongToBytes(unsignedIntMax); intValue = new byte[]{ longBytes[4], longBytes[5], longBytes[6], longBytes[7] }; maxAgeOpt = new CoAPOptionHeader(CoAPOptionName.MAX_AGE, intValue); resp.addOptionHeader(maxAgeOpt); try { assertEquals(unsignedIntMax, resp.getMaxAge()); } catch (CoAPException e) { e.printStackTrace(); } }
public long getMaxAge() throws CoAPException { int maxAge = 60; List<CoAPOptionHeader> maxAgeOption = this.getOptionHeaders(CoAPOptionName.MAX_AGE); if (maxAgeOption.size() == 1) { byte[] bytes = maxAgeOption.get(0).getValue(); if (bytes.length < 4) { ByteArrayOutputStream s = new ByteArrayOutputStream(); int diff = 4 - bytes.length; for (int i = 0; i < diff; i++) { s.write(0); } try { s.write(bytes); } catch (IOException e) { e.printStackTrace(); } bytes = s.toByteArray(); maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } else { maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } } else if (maxAgeOption.size() > 1) { throw new CoAPException("Too many max-age options found"); } return 0xffffffffL & maxAge; }
CoAPMessage { public long getMaxAge() throws CoAPException { int maxAge = 60; List<CoAPOptionHeader> maxAgeOption = this.getOptionHeaders(CoAPOptionName.MAX_AGE); if (maxAgeOption.size() == 1) { byte[] bytes = maxAgeOption.get(0).getValue(); if (bytes.length < 4) { ByteArrayOutputStream s = new ByteArrayOutputStream(); int diff = 4 - bytes.length; for (int i = 0; i < diff; i++) { s.write(0); } try { s.write(bytes); } catch (IOException e) { e.printStackTrace(); } bytes = s.toByteArray(); maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } else { maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } } else if (maxAgeOption.size() > 1) { throw new CoAPException("Too many max-age options found"); } return 0xffffffffL & maxAge; } }
CoAPMessage { public long getMaxAge() throws CoAPException { int maxAge = 60; List<CoAPOptionHeader> maxAgeOption = this.getOptionHeaders(CoAPOptionName.MAX_AGE); if (maxAgeOption.size() == 1) { byte[] bytes = maxAgeOption.get(0).getValue(); if (bytes.length < 4) { ByteArrayOutputStream s = new ByteArrayOutputStream(); int diff = 4 - bytes.length; for (int i = 0; i < diff; i++) { s.write(0); } try { s.write(bytes); } catch (IOException e) { e.printStackTrace(); } bytes = s.toByteArray(); maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } else { maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } } else if (maxAgeOption.size() > 1) { throw new CoAPException("Too many max-age options found"); } return 0xffffffffL & maxAge; } CoAPMessage(int version, CoAPMessageType messageType,
            CoAPCode methodCode, int messageId, byte[] token);  CoAPMessage(CoAPMessageType messageType, CoAPCode methodCode,
            int messageId, byte[] token); }
CoAPMessage { public long getMaxAge() throws CoAPException { int maxAge = 60; List<CoAPOptionHeader> maxAgeOption = this.getOptionHeaders(CoAPOptionName.MAX_AGE); if (maxAgeOption.size() == 1) { byte[] bytes = maxAgeOption.get(0).getValue(); if (bytes.length < 4) { ByteArrayOutputStream s = new ByteArrayOutputStream(); int diff = 4 - bytes.length; for (int i = 0; i < diff; i++) { s.write(0); } try { s.write(bytes); } catch (IOException e) { e.printStackTrace(); } bytes = s.toByteArray(); maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } else { maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } } else if (maxAgeOption.size() > 1) { throw new CoAPException("Too many max-age options found"); } return 0xffffffffL & maxAge; } CoAPMessage(int version, CoAPMessageType messageType,
            CoAPCode methodCode, int messageId, byte[] token);  CoAPMessage(CoAPMessageType messageType, CoAPCode methodCode,
            int messageId, byte[] token); void setVersion(short version); int getVersion(); void setMessageType(CoAPMessageType messageType); CoAPMessageType getMessageType(); int getOptionCount(); void setCode(CoAPCode code); CoAPCode getCode(); void setToken(byte[] token); byte[] getToken(); void setMessageId(int messageId); int getMessageId(); synchronized boolean addOptionHeader(CoAPOptionHeader option); boolean removeOptionHeader(CoAPOptionHeader option); void setPayload(byte[] payload); byte[] getPayload(); List<CoAPOptionHeader> getOptionHeaders(); List<CoAPOptionHeader> getOptionHeaders(CoAPOptionName optionName); void setOptionHeaders(List<CoAPOptionHeader> headers); int getRetransmissions(); int getTimeout(); void messageRetransmitted(); boolean messageCanceled(); void setMessageCanceled(boolean canceled); void setSocketAddress(InetSocketAddress remoteSocketAddress); InetSocketAddress getSocketAddress(); String getIdentifier(); CoAPOptionHeader getUriHostOptionHeader(); byte[] encoded(); boolean isObserveMessage(); long getMaxAge(); String logMessage(); }
CoAPMessage { public long getMaxAge() throws CoAPException { int maxAge = 60; List<CoAPOptionHeader> maxAgeOption = this.getOptionHeaders(CoAPOptionName.MAX_AGE); if (maxAgeOption.size() == 1) { byte[] bytes = maxAgeOption.get(0).getValue(); if (bytes.length < 4) { ByteArrayOutputStream s = new ByteArrayOutputStream(); int diff = 4 - bytes.length; for (int i = 0; i < diff; i++) { s.write(0); } try { s.write(bytes); } catch (IOException e) { e.printStackTrace(); } bytes = s.toByteArray(); maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } else { maxAge = BitUtil.mergeBytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); } } else if (maxAgeOption.size() > 1) { throw new CoAPException("Too many max-age options found"); } return 0xffffffffL & maxAge; } CoAPMessage(int version, CoAPMessageType messageType,
            CoAPCode methodCode, int messageId, byte[] token);  CoAPMessage(CoAPMessageType messageType, CoAPCode methodCode,
            int messageId, byte[] token); void setVersion(short version); int getVersion(); void setMessageType(CoAPMessageType messageType); CoAPMessageType getMessageType(); int getOptionCount(); void setCode(CoAPCode code); CoAPCode getCode(); void setToken(byte[] token); byte[] getToken(); void setMessageId(int messageId); int getMessageId(); synchronized boolean addOptionHeader(CoAPOptionHeader option); boolean removeOptionHeader(CoAPOptionHeader option); void setPayload(byte[] payload); byte[] getPayload(); List<CoAPOptionHeader> getOptionHeaders(); List<CoAPOptionHeader> getOptionHeaders(CoAPOptionName optionName); void setOptionHeaders(List<CoAPOptionHeader> headers); int getRetransmissions(); int getTimeout(); void messageRetransmitted(); boolean messageCanceled(); void setMessageCanceled(boolean canceled); void setSocketAddress(InetSocketAddress remoteSocketAddress); InetSocketAddress getSocketAddress(); String getIdentifier(); CoAPOptionHeader getUriHostOptionHeader(); byte[] encoded(); boolean isObserveMessage(); long getMaxAge(); String logMessage(); static final String MESSAGE_ID; static final String TOKEN; static final String URI; }