mbeckerle commented on a change in pull request #16: Implemented packed binary 
formats
URL: https://github.com/apache/incubator-daffodil/pull/16#discussion_r155306066
 
 

 ##########
 File path: 
daffodil-runtime1-unparser/src/main/scala/edu/illinois/ncsa/daffodil/processors/unparsers/PackedDecimalUnparsers.scala
 ##########
 @@ -0,0 +1,138 @@
+/* Copyright (c) 2016 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.{ 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.schema.annotation.props.gen.YesNo
+import edu.illinois.ncsa.daffodil.io.DataOutputStream
+
+abstract class PackedNumberBaseUnparser(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 PackedIntegerBaseUnparser(e: ElementRuntimeData, signed: 
Boolean)
+  extends PackedNumberBaseUnparser(e) {
+
+  override def putNumber(dos: DataOutputStream, value: JNumber, nBits: Int): 
Boolean = {
+    dos.putByteArray(packedFromBigDecimal(new JBigDecimal(value.toString), 
nBits), nBits)
+  }
+}
+
+class PackedIntegerKnownLengthUnparser(e: ElementRuntimeData, signed: Boolean, 
override val lengthInBits: Int)
+  extends PackedIntegerBaseUnparser(e, signed)
+  with HasKnownLengthInBits {
+}
+
+class PackedIntegerRuntimeLengthUnparser(val e: ElementRuntimeData, signed: 
Boolean, val lengthEv: Evaluatable[JLong], val lUnits: LengthUnits)
+  extends PackedIntegerBaseUnparser(e, signed)
+  with HasRuntimeExplicitLength {
+
+  override val runtimeDependencies = List(lengthEv)
+}
+
+class PackedDecimalKnownLengthUnparser(e: ElementRuntimeData, signed: YesNo, 
binaryDecimalVirtualPoint: Int, val lengthInBits: Int)
+  extends PackedDecimalBaseUnparser(e, signed, binaryDecimalVirtualPoint)
+  with HasKnownLengthInBits {
+}
+
+class PackedDecimalRuntimeLengthUnparser(val e: ElementRuntimeData, signed: 
YesNo, binaryDecimalVirtualPoint: Int, val lengthEv: Evaluatable[JLong], val 
lUnits: LengthUnits)
+  extends PackedDecimalBaseUnparser(e, signed, binaryDecimalVirtualPoint)
+  with HasRuntimeExplicitLength {
+
+  override val runtimeDependencies = List(lengthEv)
+}
+
+abstract class PackedDecimalBaseUnparser(e: ElementRuntimeData, signed: YesNo, 
binaryDecimalVirtualPoint: Int)
+  extends PackedNumberBaseUnparser(e) {
 
 Review comment:
   If these are needed for test cases like the formats on DFDLSchemas, or some 
of the IBM-provided tests, then we need to implement them. If not, we could 
leave these as NYI for now and implement later. 

----------------------------------------------------------------
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

Reply via email to