stevedlawrence commented on a change in pull request #16: Implemented packed binary formats URL: https://github.com/apache/incubator-daffodil/pull/16#discussion_r155267882
########## 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) + } +} + +class BCDIntegerKnownLengthUnparser(e: ElementRuntimeData, override val lengthInBits: Int) + extends BCDIntegerBaseUnparser(e) + with HasKnownLengthInBits { +} + +class BCDIntegerRuntimeLengthUnparser(val e: ElementRuntimeData, val lengthEv: Evaluatable[JLong], val lUnits: LengthUnits) + extends BCDIntegerBaseUnparser(e) + with HasRuntimeExplicitLength { + + override val runtimeDependencies = List(lengthEv) +} + +class BCDDecimalKnownLengthUnparser(e: ElementRuntimeData, binaryDecimalVirtualPoint: Int, val lengthInBits: Int) + extends BCDDecimalBaseUnparser(e, binaryDecimalVirtualPoint) + with HasKnownLengthInBits { +} + +class BCDDecimalRuntimeLengthUnparser(val e: ElementRuntimeData, binaryDecimalVirtualPoint: Int, val lengthEv: Evaluatable[JLong], val lUnits: LengthUnits) + extends BCDDecimalBaseUnparser(e, binaryDecimalVirtualPoint) + with HasRuntimeExplicitLength { + + override val runtimeDependencies = List(lengthEv) +} + +abstract class BCDDecimalBaseUnparser(e: ElementRuntimeData, binaryDecimalVirtualPoint: Int) + extends BCDNumberBaseUnparser(e) { + + override def putNumber(dos: DataOutputStream, value: JNumber, nBits: Int): Boolean = { + dos.putByteArray(bcdFromBigDecimal(new JBigDecimal(value.toString), nBits), nBits) + } +} + +final class BCDIntegerMinLengthInBytesUnparser(minLengthInBytes: Int, e: ElementRuntimeData) + extends BCDIntegerBaseUnparser(e) { + + override def getBitLength(state: ParseOrUnparseState): Int = { + val len = state.currentNode.get.asSimple.dataValue.asInstanceOf[Array[Byte]].length * 8 + val min = minLengthInBytes * 8 + scala.math.max(len, min) + } +} + +final class BCDDecimalMinLengthInBytesUnparser(minLengthInBytes: Int, e: ElementRuntimeData, binaryDecimalVirtualPoint: Int) + extends BCDDecimalBaseUnparser(e, binaryDecimalVirtualPoint) { + + override def getBitLength(state: ParseOrUnparseState): Int = { + val len = state.currentNode.get.asSimple.dataValue.asInstanceOf[Array[Byte]].length * 8 Review comment: Same question here? Is this correct or an errant copy/paste? I don't see any tests for delimited hex binary, so I suspect this isn't thoroughly tested enough? ---------------------------------------------------------------- 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
