stevedlawrence commented on code in PR #1176: URL: https://github.com/apache/daffodil/pull/1176#discussion_r1512799855
########## daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/layers/LayerDriver.scala: ########## @@ -0,0 +1,227 @@ +/* + * 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.runtime1.layers + +import java.io.FilterInputStream +import java.io.FilterOutputStream +import java.io.InputStream +import java.io.OutputStream + +import org.apache.daffodil.io.DataInputStream.Mark +import org.apache.daffodil.io.DataOutputStream +import org.apache.daffodil.io.DirectOrBufferedDataOutputStream +import org.apache.daffodil.io.FormatInfo +import org.apache.daffodil.io.InputSourceDataInputStream +import org.apache.daffodil.lib.exceptions.Assert +import org.apache.daffodil.lib.schema.annotation.props.gen.BitOrder +import org.apache.daffodil.lib.util.Maybe +import org.apache.daffodil.lib.util.Maybe.Nope +import org.apache.daffodil.lib.util.Maybe.One +import org.apache.daffodil.lib.util.Misc +import org.apache.daffodil.runtime1.layers.api.Layer +import org.apache.daffodil.runtime1.processors.unparsers.UState + +import passera.unsigned.ULong + +/** + * Driving mechanism that incorporates a layer at runtime to transform the data stream. + * + * A layer driver is created at runtime as part of a single parse/unparse call. + * Hence, they can be stateful without causing thread-safety issues. + */ +class LayerDriver( + layerRuntimeData: LayerRuntimeData, + layer: Layer, + layerVarsRuntime: LayerVarsRuntime, +) { + + private def wrapJavaInputStream( + s: InputSourceDataInputStream, + layerRuntimeImpl: LayerRuntimeImpl, + ): InputStream = + new JavaIOInputStream(s, layerRuntimeImpl.finfo) + + private def wrapJavaOutputStream( + s: DataOutputStream, + layerRuntimeImpl: LayerRuntimeImpl, + ): OutputStream = + new JavaIOOutputStream(s, layer, layerRuntimeImpl, layerVarsRuntime) + + /** + * Override these if we ever have transformers that don't have these + * requirements. + */ + val mandatoryLayerAlignmentInBits: Int = 8 + + final def addInputLayer( + s: InputSourceDataInputStream, + layerRuntimeImpl: LayerRuntimeImpl, + ): InputSourceDataInputStream = { + val jis = wrapJavaInputStream(s, layerRuntimeImpl) + val decodedInputStream = + new AssuredCloseInputStream(layer.wrapLayerInput(jis, layerRuntimeImpl), jis) + val newDIS = InputSourceDataInputStream(decodedInputStream) + newDIS.cst.setPriorBitOrder( + BitOrder.MostSignificantBitFirst, + ) // must initialize priorBitOrder + newDIS.setDebugging(s.areDebugging) + newDIS + } + + /** + * Parsing works as a tree traversal, so when the parser unwinds from + * parsing the layer we can invoke this to handle cleanups, and + * finalization issues like assigning the result variables + */ + final def removeInputLayer( + s: InputSourceDataInputStream, + layerRuntimeImpl: LayerRuntimeImpl, + ): Unit = { + layerVarsRuntime.callGettersToPopulateResultVars(layer, layerRuntimeImpl) Review Comment: The more I think about this, I actually like setters for the args and getters for the results. Using a setter gives a single place to do input error detection and avoids the dummy constructor stuff in scala implementations. Implementing a setter in scala feels a bit dirty, but this is a Java API where setters are normal. And you need mutable state for any getters, so more mutable state for setters is at least consistent. -- 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]
