br.com.objectos.rio.net.Segment Maven / Gradle / Ivy
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* Licensed 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 br.com.objectos.rio.net;
import java.nio.ByteBuffer;
import javax.xml.bind.DatatypeConverter;
/**
* @author [email protected] (Marcio Endo)
*/
abstract class Segment {
public static Segment newByte(int value) {
return new Byte(value);
}
public static Segment newByteArray(byte[] value) {
return new ByteArray(value);
}
public static Segment newInt(int value) {
return new Int(value);
}
public static Segment newShort(int value) {
return new Short(value);
}
public static Segment newZero(int length) {
return new Zero(length);
}
public abstract void accept(ByteBuffer buffer);
public abstract int size();
void accept(StringBuilder sb) {
sb.append(" ");
sb.append(getClass().getSimpleName());
sb.append(':');
sb.append(' ');
sb.append(valueToString());
}
abstract String valueToString();
private static class Byte extends Segment {
private final byte value;
public Byte(int value) {
this.value = (byte) value;
}
@Override
public void accept(ByteBuffer buffer) {
buffer.put(value);
}
@Override
public int size() {
return 1;
}
@Override
String valueToString() {
return "0x" + Integer.toHexString(value);
}
}
private static class ByteArray extends Segment {
private final byte[] value;
public ByteArray(byte[] value) {
this.value = value;
}
@Override
public void accept(ByteBuffer buffer) {
buffer.put(value);
}
@Override
public int size() {
return value.length;
}
@Override
String valueToString() {
return DatatypeConverter.printHexBinary(value);
}
}
private static class Int extends Segment {
private final int value;
public Int(int value) {
this.value = value;
}
@Override
public void accept(ByteBuffer buffer) {
buffer.putInt(value);
}
@Override
public int size() {
return 4;
}
@Override
String valueToString() {
return "0x" + Integer.toHexString(value);
}
}
private static class Short extends Segment {
private final short value;
public Short(int value) {
this.value = (short) value;
}
@Override
public void accept(ByteBuffer buffer) {
buffer.putShort(value);
}
@Override
public int size() {
return 2;
}
@Override
String valueToString() {
return "0x" + Integer.toHexString(value);
}
}
private static class Zero extends Segment {
private final int length;
private byte zero;
public Zero(int length) {
this.length = length;
}
@Override
public void accept(ByteBuffer buffer) {
for (int i = 0; i < length; i++) {
buffer.put(zero);
}
}
@Override
public int size() {
return length;
}
@Override
String valueToString() {
return "0x" + new String(new byte[length]);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy