Skip to content

Commit

Permalink
Add Bytes.
Browse files Browse the repository at this point in the history
Use it for Token and SessionId.

Signed-off-by: Achim Kraus <[email protected]>
  • Loading branch information
Achim Kraus committed Jan 30, 2019
1 parent ff2bf1c commit 06c7ad8
Show file tree
Hide file tree
Showing 26 changed files with 215 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
******************************************************************************/
package org.eclipse.californium.core.coap;

import org.eclipse.californium.elements.util.Bytes;

/**
* BlockOption represents a Block1 or Block2 option in a CoAP message.
Expand Down Expand Up @@ -163,9 +164,9 @@ public int getNum() {
*/
public byte[] getValue() {
int last = szx | (m ? 1<<3 : 0);
if (num == 0 && !m && szx==0)
return new byte[0];
else if (num < 1 << 4) {
if (num == 0 && !m && szx==0) {
return Bytes.EMPTY;
} else if (num < 1 << 4) {
return new byte[] {(byte) (last | (num << 4))};
} else if (num < 1 << 12) {
return new byte[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import java.util.Arrays;

import org.eclipse.californium.elements.util.Bytes;

/**
* Both requests and responses may include a list of one or more options. An
* Option number is constructed with a bit mask to indicate if an option is
Expand Down Expand Up @@ -75,7 +77,7 @@ public class Option implements Comparable<Option> {
* Instantiates a new empty option.
*/
public Option() {
this.value = new byte[0];
this.value = Bytes.EMPTY;
}

// Constructors
Expand All @@ -87,7 +89,7 @@ public Option() {
*/
public Option(int number) {
this.number = number;
this.value = new byte[0];
this.value = Bytes.EMPTY;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,21 @@
* Contributors:
* Bosch Software Innovations GmbH - initial API and implementation
* Achim Kraus (Bosch Software Innovations GmbH) - remove double [] from toString
* Achim Kraus (Bosch Software Innovations GmbH) - use introduced Bytes
*******************************************************************************/
package org.eclipse.californium.core.coap;

import java.util.Arrays;

import org.eclipse.californium.core.Utils;
import org.eclipse.californium.elements.util.Bytes;

/**
* Implementation of CoAP token.
*/
public class Token {
public class Token extends Bytes {

/**
* Empty token.
*/
public static final Token EMPTY = new Token(new byte[0]);

/**
* token data.
*/
private final byte[] token;
/**
* Pre-calculated hash.
*
* @see #hashCode()
*/
private final int hash;
public static final Token EMPTY = new Token(Bytes.EMPTY);

/**
* Create token from bytes.
Expand All @@ -61,75 +49,12 @@ public Token(byte[] token) {
* specified in CoAP)
*/
private Token(byte[] token, boolean copy) {
if (token == null) {
throw new NullPointerException("token bytes must not be null");
} else if (token.length > 8) {
throw new IllegalArgumentException("Token length must be between 0 and 8 inclusive");
}
if (copy && token.length > 0) {
this.token = Arrays.copyOf(token, token.length);
} else {
this.token = token;
}
this.hash = Arrays.hashCode(this.token);
super(token, 8, copy);
}

@Override
public String toString() {
return new StringBuilder("Token=").append(Utils.toHexString(token)).toString();
}

@Override
public int hashCode() {
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Token other = (Token) obj;
return Arrays.equals(token, other.token);
}

/**
* Get token bytes.
*
* @return token bytes. Not Copied!
*/
public byte[] getBytes() {
return token;
}

/**
* Get token bytes as (hexadecimal) string.
*
* @return token bytes as (hexadecimal) string
*/
public String getAsString() {
return Utils.toHexString(token);
}

/**
* Check, if token is empty.
*
* @return {@code true}, if token is empty, {@code false}, otherwise
*/
public boolean isEmpty() {
return token.length == 0;
}

/**
* Return number of token bytes.
*
* @return number of token bytes. 0 to 8.
*/
public int length() {
return token.length;
return new StringBuilder("Token=").append(getAsString()).toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.californium.category.Small;
import org.eclipse.californium.core.coap.Option;
import org.eclipse.californium.core.coap.OptionSet;
import org.eclipse.californium.elements.util.Bytes;
import org.eclipse.californium.core.coap.OptionNumberRegistry;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -252,7 +253,7 @@ public void testSetStringValue() {
Option option = new Option();

option.setStringValue("");
assertArrayEquals(option.getValue(), new byte[0]);
assertArrayEquals(option.getValue(), Bytes.EMPTY);

option.setStringValue("Californium");
assertArrayEquals(option.getValue(), "Californium".getBytes());
Expand All @@ -263,7 +264,7 @@ public void testSetIntegerValue() {
Option option = new Option();

option.setIntegerValue(0);
assertArrayEquals(option.getValue(), new byte[0]);
assertArrayEquals(option.getValue(), Bytes.EMPTY);
assertEquals(0, option.getIntegerValue());

option.setIntegerValue(11);
Expand Down Expand Up @@ -300,7 +301,7 @@ public void testSetLongValue() {
Option option = new Option();

option.setLongValue(0);
assertArrayEquals(option.getValue(), new byte[0]);
assertArrayEquals(option.getValue(), Bytes.EMPTY);
assertEquals(0, option.getLongValue());

option.setLongValue(11);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ public void set(Message message) {
public class EmptyMessageProperty extends MessageProperty {

public EmptyMessageProperty(Type type, int mid) {
super(type, new Token(new byte[0]), mid);
super(type, Token.EMPTY, mid);
}

public EmptyMessageProperty(Type type, String midVar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.util.Bytes;


/**
* This resource implements a test of specification for the ETSI IoT CoAP Plugtests, London, UK, 7--9 Mar 2014.
*/
public class Create extends CoapResource {
private static final byte[] EMPTY = new byte[0];

// Members ////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -82,7 +82,7 @@ public void handleDELETE(CoapExchange exchange) {
private synchronized void storeData(byte[] payload, int cf) {

// set payload and content type
data = payload != null ? payload : EMPTY;
data = payload != null ? payload : Bytes.EMPTY;
dataCf = cf;
getAttributes().clearContentType();
getAttributes().addContentType(dataCf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import org.eclipse.californium.core.coap.LinkFormat;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.util.Bytes;

/**
* This resource implements a test of specification for the
* ETSI IoT CoAP Plugtests, London, UK, 7--9 Mar 2014.
*/
public class LargeCreate extends CoapResource {
private static final byte[] EMPTY = new byte[0];

// Members /////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -87,7 +87,7 @@ private class StorageResource extends CoapResource {
public StorageResource(String name, byte[] post, int ct) {
super(name);

this.data = post != null ? post : EMPTY;
this.data = post != null ? post : Bytes.EMPTY;
this.dataCt = ct;

getAttributes().addContentType(dataCt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.util.Bytes;

/**
* This resource implements a test of specification for the
* ETSI IoT CoAP Plugtests, London, UK, 7--9 Mar 2014.
*/
public class LargeUpdate extends CoapResource {
private static final byte[] EMPTY = new byte[0];

// Members ////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -110,7 +110,7 @@ public void handlePUT(CoapExchange exchange) {
private synchronized void storeData(byte[] payload, int cf) {

// set payload and content type
data = payload != null ? payload : EMPTY;
data = payload != null ? payload : Bytes.EMPTY;
dataCf = cf;

getAttributes().clearContentType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.CoAP.Type;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.util.Bytes;

import static org.eclipse.californium.core.coap.CoAP.ResponseCode.*;
import static org.eclipse.californium.core.coap.MediaTypeRegistry.*;
Expand All @@ -33,7 +34,6 @@
* ETSI IoT CoAP Plugtests, London, UK, 7--9 Mar 2014.
*/
public class Observe extends CoapResource {
private static final byte[] EMPTY = new byte[0];

// Members ////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -140,7 +140,7 @@ private synchronized void storeData(byte[] payload, int format) {
}

// set payload and content type
data = payload != null ? payload : EMPTY;
data = payload != null ? payload : Bytes.EMPTY;
dataCf = format;

getAttributes().clearContentType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.CoAP.Type;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.util.Bytes;

import static org.eclipse.californium.core.coap.CoAP.ResponseCode.*;
import static org.eclipse.californium.core.coap.MediaTypeRegistry.*;
Expand All @@ -34,7 +35,6 @@
* ETSI IoT CoAP Plugtests, London, UK, 7--9 Mar 2014.
*/
public class ObserveNon extends CoapResource {
private static final byte[] EMPTY = new byte[0];

// Members ////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -141,7 +141,7 @@ private synchronized void storeData(byte[] payload, int format) {
}

// set payload and content type
data = payload != null ? payload : EMPTY;
data = payload != null ? payload : Bytes.EMPTY;
dataCf = format;

getAttributes().clearContentType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.eclipse.californium.elements.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -365,7 +367,7 @@ private class Sender extends NetworkStageThread {

private Sender(String name) {
super(name);
this.datagram = new DatagramPacket(new byte[0], 0);
this.datagram = new DatagramPacket(Bytes.EMPTY, 0);
}

protected void work() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import javax.security.auth.x500.X500Principal;

import org.eclipse.californium.elements.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -184,7 +185,7 @@ public byte[] toByteArray() {
} catch (CertificateEncodingException e) {
// should not happen because all Java 7 implementations are required
// to support PkiPath encoding of X.509 certificates
return new byte[0];
return Bytes.EMPTY;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ public static byte[] decode( byte[] source, int off, int len, int options )
} // end if

if( len == 0 ){
return new byte[0];
return Bytes.EMPTY;
}else if( len < 4 ){
throw new IllegalArgumentException(
"Base64-encoded string must have at least four characters, but length specified was " + len );
Expand Down
Loading

0 comments on commit 06c7ad8

Please sign in to comment.