stevedlawrence commented on a change in pull request #16: Implemented packed binary formats URL: https://github.com/apache/incubator-daffodil/pull/16#discussion_r163049903
########## File path: daffodil-runtime1/src/main/scala/edu/illinois/ncsa/daffodil/processors/parsers/PackedBinaryTraits.scala ########## @@ -0,0 +1,195 @@ +/* Copyright (c) 2012-2014 Tresys Technology, LLC. All rights reserved. + * + * Developed by: Tresys Technology, LLC + * http://www.tresys.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal with + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the names of Tresys Technology, nor the names of its contributors + * may be used to endorse or promote products derived from this Software + * without specific prior written permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE + * SOFTWARE. + */ + +package edu.illinois.ncsa.daffodil.processors.parsers + +import edu.illinois.ncsa.daffodil.processors.ElementRuntimeData +import edu.illinois.ncsa.daffodil.processors.ParseOrUnparseState +import edu.illinois.ncsa.daffodil.processors.FieldDFAParseEv +import edu.illinois.ncsa.daffodil.processors.TextJustificationType +import edu.illinois.ncsa.daffodil.processors.dfa.TextDelimitedParserBase +import edu.illinois.ncsa.daffodil.util.Maybe +import edu.illinois.ncsa.daffodil.exceptions.Assert +import edu.illinois.ncsa.daffodil.equality._ +import edu.illinois.ncsa.daffodil.processors.dfa +import edu.illinois.ncsa.daffodil.util.MaybeChar +import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.BinaryNumberCheckPolicy +import java.math.{ BigInteger => JBigInteger, BigDecimal => JBigDecimal } +import java.nio.charset.StandardCharsets +import passera.unsigned.ULong + +trait PackedBinaryConversion { + def toBigInteger(num: Array[Byte]): JBigInteger + def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal +} + +abstract class PackedBinaryDecimalBaseParser( + e: ElementRuntimeData, + binaryDecimalVirtualPoint: Int) + extends PrimParserObject(e) + with PackedBinaryConversion { + + protected def getBitLength(s: ParseOrUnparseState): Int + + def parse(start: PState): Unit = { + val nBits = getBitLength(start) + if (nBits == 0) return // zero length is used for outputValueCalc often. + val dis = start.dataInputStream + + if (!dis.isDefinedForLength(nBits)) { + PE(start, "Insufficient bits in data. Needed %d bit(s) but found only %d available.", nBits, dis.remainingBits.get) + return + } + + try { + val bigDec = toBigDecimal(dis.getByteArray(nBits, start), binaryDecimalVirtualPoint) + start.simpleElement.overwriteDataValue(bigDec) + } catch { + case n: NumberFormatException => PE(start, "NumberFormatException in packed data: \n%s", n.getMessage()) + } Review comment: This is really minor, but it's probably more correct to move the overwriteDataValue outside of the try/catch block. That function shouldn't throw an NFE, and if it did, it's probably a bug that we do not want to hide in a ParseError. So instead, you can do something like this: ```scala val bigDec = try { toBigDecimal(...) } catch { case ... } start.simpleElement.overwriteDataValue(bigDec) ``` Also, it's generally good to avoid mentioning interal class names like NumberFormatException where possible, just because that doesn't mean anything to a user. Can the error message be changed to something a little more user friendly? Maybe just "Error in packed data:"? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
