stevedlawrence commented on code in PR #1176:
URL: https://github.com/apache/daffodil/pull/1176#discussion_r1512835778


##########
daffodil-runtime1-layers/src/main/scala/org/apache/daffodil/layers/runtime1/FixedLengthLayer.scala:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.daffodil.layers.runtime1
+
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+import java.io.InputStream
+import java.io.OutputStream
+import java.lang.{ Long => JLong }
+import java.nio.ByteBuffer
+
+import org.apache.daffodil.lib.exceptions.Assert
+import org.apache.daffodil.runtime1.layers.api.Layer
+import org.apache.daffodil.runtime1.layers.api.LayerRuntime
+
+import org.apache.commons.io.IOUtils
+
+/**
+ * Suitable only for small sections of data, not large data streams or whole 
files.
+ * See the maxFixedLength value defined herein for the maximum.
+ *
+ * The entire fixed length region of the data will be pulled into a byte 
buffer in memory.
+ *
+ * TODO: Someday, enhance to make this streaming.
+ *
+ * One DFDL Variable is a parameter
+ *   - fixedLength - an unsignedInt giving the fixed length of this layer.
+ *   This length is enforced on both parsing and unparsing the layer.
+ * There are no output/result DFDL variables from this layer.
+ */
+final class FixedLengthLayer(var fixedLength: JLong)
+  extends Layer("fixedLength", "urn:org.apache.daffodil.layers.fixedLength") {
+
+  Assert.invariant(fixedLength > 0)
+
+  /** Required for SPI class loading */
+  def this() = this(1)
+
+  private def maxFixedLength = Short.MaxValue
+
+  override def wrapLayerInput(jis: InputStream, lr: LayerRuntime): InputStream 
= {
+
+    if (fixedLength > maxFixedLength)
+      lr.processingError(
+        s"fixedLength value of $fixedLength is above the maximum of 
$maxFixedLength.",
+      )
+
+    new FixedLengthInputStream(fixedLength.toInt, jis, lr)
+  }
+
+  override def wrapLayerOutput(jos: OutputStream, lr: LayerRuntime): 
OutputStream = {
+
+    if (fixedLength > maxFixedLength)

Review Comment:
   I think maybe I was unclear. The duplicated logic I meant is how both wrap() 
functions have to check the input variables for correctness (i.e. less than 
maxFixedLength). We can't really do those checks the constructor because it 
doesn't have access to LayerRuntime. A setDFDLVariables function might be the 
solution.
   
   The checks you're talking about make sense to me (though I think could maybe 
be avoided with some transient stuff, but I'll have to think more about this 
with the next cut).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to