mbeckerle commented on a change in pull request #61: Preliminary Review - 
Base64 layering runs first parsing unit test.
URL: https://github.com/apache/incubator-daffodil/pull/61#discussion_r181540295
 
 

 ##########
 File path: 
daffodil-io/src/main/scala/org/apache/daffodil/io/LimitingJavaIOStreams.scala
 ##########
 @@ -0,0 +1,281 @@
+/*
+ * 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.io
+
+import org.apache.daffodil.exceptions.Assert
+import java.util.regex.Pattern
+import java.util.Scanner
+import java.nio.charset.StandardCharsets
+import java.nio.charset.Charset
+import java.io.InputStream
+import java.io.FilterInputStream
+import java.io.InputStreamReader
+
+/**
+ * This class can be used with any InputStream to restrict what is
+ * read from it to N bytes.
+ *
+ * This can be used to forcibly stop consumption of data from a stream at
+ * a length obtained explicitly.
+ *
+ * Thread safety: This is inherently stateful - so not thread safe to use
+ * this object from more than one thread.
+ */
+class ExplicitLengthLimitingStream(in: InputStream, limit: Int)
+  extends FilterInputStream(in) {
+
+  private var numRemaining = limit
+
+  override def read(buf: Array[Byte], off: Int, len: Int) = {
+    Assert.invariant(numRemaining >= 0)
+    if (numRemaining == 0) -1
+    else if (len == 0) 0
+    else {
+      val requestSize = math.min(numRemaining, len)
+      val actualSize = in.read(buf, off, requestSize)
+      if (actualSize == -1)
+        numRemaining = 0
+      else
+        numRemaining -= actualSize
+      actualSize
+    }
+  }
+
+  private val readBuf = new Array[Byte](1)
+
+  override def read(): Int = {
+    readBuf(0) = 0
+    val n = read(readBuf, 0, 1)
+    if (n == -1)
+      -1
+    else {
+      Assert.invariant(n == 1)
+      val i = readBuf(0).toInt
+      val b = i & 0xFF
+      b
+    }
+  }
+}
+
+/**
+ * This class can be used with any InputStream to restrict what is
+ * read from it to stop before a terminator string.
+ *
+ * The terminator string is exactly that, a string of characters. Not a
+ * regex, nor anything involving DFDL Character Entities or Character Class
+ * Entities. (No %WSP; no %NL; )
+ *
+ * This can be used to forcibly stop consumption of data from a stream at
+ * a length obtained from a delimiter.
+ *
+ * The terminator string is consumed from the underlying stream (if found), and
+ * the underlying stream is left positioned at the byte after the terminator
+ * string.
+ *
+ * Thread safety: This is inherently stateful - so not thread safe to use
+ * this object from more than one thread.
+ */
+class DelimitedLengthLimitingStream(inWithMarkSupported: InputStream, 
terminator: String,
+  charset: Charset = StandardCharsets.ISO_8859_1,
+  maxChunkSize: Int = 1024 * 32) // 32K chunk size by default. Units are chars.
+  extends FilterInputStream(inWithMarkSupported) {
+  Assert.usage(maxChunkSize >= 1)
+
+  private val charsetDecoder = charset.newDecoder()
+  private val rdr = new InputStreamReader(in, charsetDecoder)
+  private val maxBytesPerChar = charset.newEncoder().maxBytesPerChar()
+
+  /**
+   * We will create a new scanner for the rdr each read call, because
+   * scanners are stateful.
+   */
+  private var scanner: Scanner = _ // TODO: Performance... how to avoid this 
allocation overhead? Implement our own scanner?
+
+  /**
+   * This pattern uses unsafe ".*" but since it is always used along with
+   * the scanner methods that take a horizon limit, it is safe.
+   *
+   * Pattern matches the shortest string of anything followed by the 
terminator.
+   * The part preceding the terminator goes into capture group 1.
+   *
+   * After we match this regex with the scanner,
+   * the input stream should end up positioned immediately before the 
terminator.
+   * The terminator is not part of the match, because it is in a non-capturing
+   * lookahead group.
+   *
+   * However, for assertion checking, a second capture group is defined
+   * inside the lookahead, and that should capture the terminator.
+   */
+  private val quotedTerminator = Pattern.quote(terminator) // in case pattern 
has non-regex-safe characters in it
 
 Review comment:
   No specific reason. I was hoping that the scan for terminator string of a 
base64 region would be very lightweight and fast. E.g., I'm thinking 
layerTerminator is just a plain string, no WSP or other class entities, etc. 
That generality may not be needed, and the test burden that generality creates 
is not insubstantial. 
   
   We'll revisit this point. 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to