Java Code Examples for com.igormaznitsa.jbbp.io.JBBPByteOrder#LITTLE_ENDIAN

The following examples show how to use com.igormaznitsa.jbbp.io.JBBPByteOrder#LITTLE_ENDIAN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JBBPTextWriter.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
/**
 * Print short value.
 *
 * @param value short value to be printed
 * @return the context
 * @throws IOException it will be thrown for transport errors
 */
public JBBPTextWriter Short(final int value) throws IOException {
  ensureValueMode();
  String convertedByExtras = null;
  for (final Extra e : this.extras) {
    convertedByExtras = e.doConvertShortToStr(this, value & 0xFFFF);
    if (convertedByExtras != null) {
      break;
    }
  }

  if (convertedByExtras == null) {
    final long valueToWrite;
    if (this.byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
      valueToWrite = JBBPUtils.reverseByteOrder(value, 2);
    } else {
      valueToWrite = value;
    }
    printValueString(JBBPUtils.ensureMinTextLength(JBBPUtils.ulong2str(valueToWrite & 0xFFFFL, this.radix, CHAR_BUFFER), this.maxCharsRadixForShort, '0', 0));
  } else {
    printValueString(convertedByExtras);
  }
  return this;
}
 
Example 2
Source File: JBBPTextWriter.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
/**
 * Print float value.
 *
 * @param value float value to be printed
 * @return the context
 * @throws IOException it will be thrown for transport errors
 * @since 1.4.0
 */
public JBBPTextWriter Float(final float value) throws IOException {
  ensureValueMode();
  String convertedByExtras = null;
  for (final Extra e : this.extras) {
    convertedByExtras = e.doConvertFloatToStr(this, value);
    if (convertedByExtras != null) {
      break;
    }
  }

  if (convertedByExtras == null) {
    final float valueToWrite;
    if (this.byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
      valueToWrite = Float.intBitsToFloat((int) JBBPFieldInt.reverseBits(Float.floatToIntBits(value)));
    } else {
      valueToWrite = value;
    }
    printValueString(JBBPUtils.ensureMinTextLength(JBBPUtils.float2str(valueToWrite, this.radix), this.maxCharsRadixForShort, '0', 0));
  } else {
    printValueString(convertedByExtras);
  }
  return this;
}
 
Example 3
Source File: JBBPTextWriter.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
/**
 * Print double value.
 *
 * @param value double value to be printed
 * @return the context
 * @throws IOException it will be thrown for transport errors
 * @since 1.4.0
 */
public JBBPTextWriter Double(final double value) throws IOException {
  ensureValueMode();
  String convertedByExtras = null;
  for (final Extra e : this.extras) {
    convertedByExtras = e.doConvertDoubleToStr(this, value);
    if (convertedByExtras != null) {
      break;
    }
  }

  if (convertedByExtras == null) {
    final double valueToWrite;
    if (this.byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
      valueToWrite = Double.longBitsToDouble(JBBPFieldLong.reverseBits(Double.doubleToLongBits(value)));
    } else {
      valueToWrite = value;
    }
    printValueString(JBBPUtils.ensureMinTextLength(JBBPUtils.double2str(valueToWrite, this.radix), this.maxCharsRadixForShort, '0', 0));
  } else {
    printValueString(convertedByExtras);
  }
  return this;
}
 
Example 4
Source File: JBBPTextWriter.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
/**
 * Print integer value
 *
 * @param value value to be printed
 * @return the context
 * @throws IOException it will be thrown for transport error
 */
public JBBPTextWriter Int(final int value) throws IOException {
  ensureValueMode();

  String convertedByExtras = null;
  for (final Extra e : this.extras) {
    convertedByExtras = e.doConvertIntToStr(this, value);
    if (convertedByExtras != null) {
      break;
    }
  }

  if (convertedByExtras == null) {
    final long valueToWrite;
    if (this.byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
      valueToWrite = JBBPUtils.reverseByteOrder(value, 4);
    } else {
      valueToWrite = value;
    }
    printValueString(JBBPUtils.ensureMinTextLength(JBBPUtils.ulong2str(valueToWrite & 0xFFFFFFFFL, this.radix, CHAR_BUFFER), this.maxCharsRadixForInt, '0', 0));
  } else {
    printValueString(convertedByExtras);
  }
  return this;
}
 
Example 5
Source File: JBBPTextWriter.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
/**
 * Print long value
 *
 * @param value value to be printed
 * @return the context
 * @throws IOException it will be thrown for transport errors
 */
public JBBPTextWriter Long(final long value) throws IOException {
  ensureValueMode();

  String convertedByExtras = null;
  for (final Extra e : this.extras) {
    convertedByExtras = e.doConvertLongToStr(this, value);
    if (convertedByExtras != null) {
      break;
    }
  }

  if (convertedByExtras == null) {
    final long valueToWrite;
    if (this.byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
      valueToWrite = JBBPUtils.reverseByteOrder(value, 8);
    } else {
      valueToWrite = value;
    }
    printValueString(JBBPUtils.ensureMinTextLength(JBBPUtils.ulong2str(valueToWrite, this.radix, CHAR_BUFFER), this.maxCharsRadixForLong, '0', 0));
  } else {
    printValueString(convertedByExtras);
  }
  return this;
}
 
Example 6
Source File: JBBPFieldTypeParameterContainer.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
@Override
public String toString() {
  final StringBuilder result = new StringBuilder();

  if (byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
    result.append('<');
  }
  result.append(this.typeName);
  if (extraData != null) {
    int insertIndex = typeName.indexOf(' ');
    if (insertIndex < 0) {
      insertIndex = result.length();
    } else {
      insertIndex++;
    }
    result.insert(insertIndex, ':' + extraData);

  }

  return result.toString();
}
 
Example 7
Source File: JBBPDslBuilderTest.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnotatedClass_AnnottatedButWithoutType() {
  class Test {
    @Bin(order = 1)
    int a;
    @Bin(order = 3)
    int c;
    @Bin(order = 2, byteOrder = JBBPByteOrder.LITTLE_ENDIAN)
    int b;
    @Bin(order = 4, arraySizeExpr = "a+b")
    Internal[] d;

    class Internal {
      @Bin(order = 1)
      short a;
      @Bin(order = 2, arraySizeExpr = "8")
      short[] b;
    }
  }

  assertEquals("Test{int a;<int b;int c;d[a+b]{short a;short[8] b;}}", Begin().AnnotatedClass(Test.class).End());
}
 
Example 8
Source File: JBBPTextWriterTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructor_WriterAndByteOrder() {
  final Writer someWriter = new StringWriter();
  final JBBPTextWriter writer = new JBBPTextWriter(someWriter, JBBPByteOrder.LITTLE_ENDIAN);
  assertSame(someWriter, writer.getWrappedWriter());
  assertEquals(JBBPByteOrder.LITTLE_ENDIAN, writer.getByteOrder());
}
 
Example 9
Source File: PackedBCDCustomFieldTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isAllowed(final JBBPFieldTypeParameterContainer fieldType, final String fieldName, final int extraData, final boolean isArray) {
  if (fieldType.getByteOrder() == JBBPByteOrder.LITTLE_ENDIAN) {
    System.err.println("Packed Decimal does not support little endian...using big endian instead");
    return false;
  }

  return extraData > 0 && extraData < 15;
}
 
Example 10
Source File: JBBPDslBuilder.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  String type;
  boolean isArray;
  boolean customType = this instanceof ItemCustom;

  if (customType) {
    type = ((ItemCustom) this).customType;
    isArray = ((ItemCustom) this).array;
  } else {
    type = this.type.name().toLowerCase(Locale.ENGLISH);
    isArray = type.endsWith("_array");

    if (isArray) {
      type = type.substring(0, type.indexOf('_'));
    }

    if (type.equals("string") || type.equals("float") || type.equals("double")) {
      type += 'j';
    }
  }

  final StringBuilder result = new StringBuilder();

  if (this.byteOrder == JBBPByteOrder.LITTLE_ENDIAN) {
    result.append('<');
  }

  result.append(type);

  if (customType) {
    if (this.bitLenExpression != null) {
      result.append(':').append(makeExpressionForExtraField(this.bitLenExpression));
    }
  } else if (this.type == BinType.BIT || this.type == BinType.BIT_ARRAY) {
    result.append(':');
    if (bitLenExpression == null) {
      if (this.bitNumber == null) {
        result.append('1');
      } else {
        result.append(this.bitNumber.getBitNumber());
      }
    } else {
      result.append(makeExpressionForExtraField(this.bitLenExpression));
    }
  }

  if (isArray) {
    result.append('[').append(this.sizeExpression).append(']');
  }

  if (this.name != null && this.name.length() != 0) {
    result.append(' ').append(this.name);
  }

  result.append(';');

  return result.toString();
}