Created
August 3, 2017 15:55
-
-
Save alastairmccormack/ac1539784af7ae7569cd50135016cf2c to your computer and use it in GitHub Desktop.
Java Binary Block Parser (JBBP) unsigned int 32 parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.igormaznitsa.jbbp.JBBPCustomFieldTypeProcessor; | |
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo; | |
import com.igormaznitsa.jbbp.compiler.tokenizer.JBBPFieldTypeParameterContainer; | |
import com.igormaznitsa.jbbp.io.JBBPBitInputStream; | |
import com.igormaznitsa.jbbp.io.JBBPBitOrder; | |
import com.igormaznitsa.jbbp.io.JBBPByteOrder; | |
import com.igormaznitsa.jbbp.model.*; | |
import java.io.EOFException; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
/** | |
* A custom processor for Java Binary Block Parser (JBBP) | |
* to read unsigned int 32 bytes (uint32) and return them as longs | |
* | |
* @author alastair.mccormack | |
*/ | |
public final class Uint32 implements JBBPCustomFieldTypeProcessor { | |
private static final String[] TYPES = new String[]{"uint32"}; | |
private static long uint32_read(final JBBPBitInputStream in, final JBBPByteOrder byteOrder, final JBBPBitOrder bitOrder) throws IOException { | |
final int signedInt = in.readInt(byteOrder); | |
/* Java 8 | |
return Integer.toUnsignedLong(signedInt);*/ | |
// Java * | |
return signedInt & 0xffffffffL; | |
} | |
@Override | |
public String[] getCustomFieldTypes() { | |
return TYPES; | |
} | |
@Override | |
public boolean isAllowed(final JBBPFieldTypeParameterContainer fieldType, final String fieldName, final int extraData, final boolean isArray) { | |
return extraData == 0; | |
} | |
/** | |
* Converts List of Longs to long[] | |
* | |
* @param longs A List of Longs | |
* @return | |
*/ | |
private long[] convertLongs(List<Long> longs) | |
{ | |
long[] ret = new long[longs.size()]; | |
Iterator<Long> iterator = longs.iterator(); | |
for (int i = 0; i < ret.length; i++) | |
{ | |
ret[i] = iterator.next().longValue(); | |
} | |
return ret; | |
} | |
@Override | |
public JBBPAbstractField readCustomFieldType(final JBBPBitInputStream in, final JBBPBitOrder bitOrder, | |
final int parserFlags, final JBBPFieldTypeParameterContainer customTypeFieldInfo, | |
final JBBPNamedFieldInfo fieldName, final int extraData, | |
final boolean readWholeStream, final int arrayLength) throws IOException { | |
// read just one | |
if (arrayLength < 0) { | |
final long uint32_val = uint32_read(in, customTypeFieldInfo.getByteOrder(), bitOrder); | |
return new JBBPFieldLong(fieldName, uint32_val); | |
} else { | |
// read multiple | |
// if [_] given | |
if (readWholeStream) { | |
// the Inner Circle | |
ArrayList<Long> laLaLaLaLong = new ArrayList<Long>(); | |
// Read as much as possible | |
try { | |
while (true) { | |
laLaLaLaLong.add(uint32_read(in, customTypeFieldInfo.getByteOrder(), bitOrder)); | |
} | |
} catch (EOFException e) { | |
} | |
long longs[] = convertLongs(laLaLaLaLong); | |
return new JBBPFieldArrayLong(fieldName, longs); | |
} else { | |
// [n] given | |
final long[] array = new long[arrayLength]; | |
for (int i = 0; i < arrayLength; i++) { | |
array[i] = uint32_read(in, customTypeFieldInfo.getByteOrder(), bitOrder); | |
} | |
return new JBBPFieldArrayLong(fieldName, array); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment