stevedlawrence commented on a change in pull request #16: Implemented packed binary formats URL: https://github.com/apache/incubator-daffodil/pull/16#discussion_r155265041
########## File path: daffodil-runtime1-unparser/src/main/scala/edu/illinois/ncsa/daffodil/processors/unparsers/BCDUnparsers.scala ########## @@ -0,0 +1,135 @@ +/* Copyright (c) 2017 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.unparsers + +import edu.illinois.ncsa.daffodil.processors.parsers.HasKnownLengthInBits +import edu.illinois.ncsa.daffodil.processors.ElementRuntimeData +import edu.illinois.ncsa.daffodil.processors.ParseOrUnparseState +import edu.illinois.ncsa.daffodil.util.Maybe._ +import edu.illinois.ncsa.daffodil.util.DecimalUtils._ +import java.lang.{ Number => JNumber, Long => JLong } +import java.math.{ BigInteger => JBigInteger, BigDecimal => JBigDecimal } +import edu.illinois.ncsa.daffodil.exceptions.Assert +import edu.illinois.ncsa.daffodil.processors.parsers.HasRuntimeExplicitLength +import edu.illinois.ncsa.daffodil.processors.Evaluatable +import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.LengthUnits +import edu.illinois.ncsa.daffodil.io.DataOutputStream + +abstract class BCDNumberBaseUnparser(e: ElementRuntimeData) + extends PrimUnparserObject(e) { + + protected def getBitLength(s: ParseOrUnparseState): Int + + protected def putNumber(dos: DataOutputStream, number: JNumber, nBits: Int): Boolean + + def unparse(state: UState): Unit = { + val nBits = getBitLength(state) + val node = state.currentInfosetNode.asSimple + val value = node.dataValue.asInstanceOf[JNumber] + val dos = state.dataOutputStream + + val res = + if (nBits > 0) { + putNumber(dos, value, nBits) + } else { + true + } + + if (!res) { + Assert.invariant(dos.maybeRelBitLimit0b.isDefined) + UnparseError(One(state.schemaFileLocation), One(state.currentLocation), "Insufficient space to unparse element %s, required %s bits, but only %s were available.", + e.dpathElementCompileInfo.namedQName.toPrettyString, nBits, dos.maybeRelBitLimit0b.get) + } + } + +} + +abstract class BCDIntegerBaseUnparser(e: ElementRuntimeData) + extends BCDNumberBaseUnparser(e) { + + override def putNumber(dos: DataOutputStream, value: JNumber, nBits: Int): Boolean = { + dos.putByteArray(bcdFromBigInteger(new JBigInteger(value.toString), nBits), nBits) Review comment: This converts value to a string, then converts that string to a JBigInteger, then the first thing bcdFromBigInteger does is convert that JBigInteger to a String/Array[Char]. Would it make sense to change bcdFromBigInteger to just accept a String and avoid the creation of a JBigInteger if we're just going to convert it back to a string? Also, it might be a bit more readable to split this online into 2 or 3 lines/vals. ---------------------------------------------------------------- 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
