You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2019/11/20 15:37:28 UTC

[GitHub] [incubator-daffodil] stevedlawrence commented on a change in pull request #291: Switch to file output stream for large data

stevedlawrence commented on a change in pull request #291: Switch to file output stream for large data
URL: https://github.com/apache/incubator-daffodil/pull/291#discussion_r348523311
 
 

 ##########
 File path: daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStreamImplMixin.scala
 ##########
 @@ -657,6 +668,64 @@ trait DataOutputStreamImplMixin extends DataStreamCommonState
     }
   }
 
+  final def putFile(
+    path: Path,
+    lengthInBits: Long,
+    chunkSizeInBytes: Int,
+    finfo: FormatInfo): Long = {
+
+    val fileStream =
+      try {
+        Files.newInputStream(path, StandardOpenOption.READ)
+      } catch {
+        case e: Exception =>
+          throw new BlobIOException(
+            "Unable to open BLOB %s for reading: %s".format(
+              path.toString,
+              e.getMessage()))
+      }
+
+    val array = new Array[Byte](chunkSizeInBytes)
+
+    val chunkSizeInBits = chunkSizeInBytes * 8
+
+    var remainingBitsToPut = lengthInBits
+    var fileHasData = true
+    var bitsWritten: Long = 0
+    while (remainingBitsToPut > 0 && fileHasData) {
+      val bitsToRead = Math.min(remainingBitsToPut, chunkSizeInBits)
+      val bytesToRead = (bitsToRead + 7) / 8
+      val bytesRead = fileStream.read(array, 0, bytesToRead.toInt)
+      if (bytesRead == -1) {
+        fileHasData = false
+      } else {
+        val bitsToPut = Math.min(bytesRead * 8, bitsToRead)
+        val ret = putByteArray(array, bitsToPut.toInt, finfo, ignoreByteOrder=true)
+        if (!ret) {
+          fileStream.close()
+          throw new BlobIOException(
+            "Failed to write BLOB data: %s".format(path.toString))
+        }
+        bitsWritten += bitsToPut
+        remainingBitsToPut -= bitsToPut
+      }
+    }
+
+    fileStream.close()
+
+    // calculate the skip bits
+    val nFillBits = remainingBitsToPut
+    if (nFillBits > 0) {
+      val ret = skip(nFillBits, finfo)
+      if (!ret) {
+          throw new BlobIOException(
+            "Failed to skip %s bits after BLOB data: %s".format(nFillBits, path.toString))
+      }
+    }
+    bitsWritten / 8
 
 Review comment:
   Should this return bitsWritten instead of of bytes written? That way if a caller wants to know if it succeeded they can check with the value that they passed in. Or maybe just return a boolean for success/failure?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services