Skip to content

Commit

Permalink
fixed ReadLength implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 2, 2018
1 parent 46fa650 commit d0e3445
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ public class ReadLength extends ByteValue {
assertLength(NUM_BYTES);
}

public static ReadLength of(int length) {
return new ReadLength(ByteBuffer.allocate(NUM_BYTES).putInt(length).array());
public static ReadLength of(long length) {
checkUnsignedBounds(length, NUM_BYTES);
return new ReadLength(ByteBuffer.allocate(NUM_BYTES)
.put((byte) (length >> 24 & 0xff))
.put((byte) (length >> 16 & 0xff))
.put((byte) (length >> 8 & 0xff))
.put((byte) (length & 0xff))
.array());
}

public static ReadLength of(String length) {
return of(Long.parseLong(length));
}

public static ReadLength of(byte... values) {
return new ReadLength(values);
}

@Override
public String toString() {
return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.plc4x.java.ads.api.commands.types;

import org.apache.commons.codec.binary.Hex;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class ReadReadLengthTest {
byte NULL_BYTE = 0x0;

@Test
void ofBytes() {
Assertions.assertEquals("0", ReadLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString());
Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE));
}

@Test
void ofLong() {
assertByte(ReadLength.of(1), "0x00000001");
assertByte(ReadLength.of(65535), "0x0000ffff");
Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(-1));
Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(Long.valueOf("4294967296")));
}

@Test
void ofString() {
assertByte(ReadLength.of("1"), "0x00000001");
}

@Test
void testToString() {
Assertions.assertEquals(ReadLength.of("1").toString(), "1");
}

void assertByte(ReadLength actual, String expected) {
Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes()));
}
}

0 comments on commit d0e3445

Please sign in to comment.