Skip to content

Commit

Permalink
eliminated documentInputStreamIsConsumed
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Dec 22, 2024
1 parent fb1805d commit 5fd89e7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ abstract class IppMessage() {
}
val attributesGroups = mutableListOf<IppAttributesGroup>()
var documentInputStream: InputStream? = null
var documentInputStreamIsConsumed: Boolean = false
var rawBytes: ByteArray? = null
var documentBytes: ByteArray? = null

Expand Down Expand Up @@ -76,7 +75,7 @@ abstract class IppMessage() {
fun createAttributesGroup(tag: IppTag) =
IppAttributesGroup(tag).apply { attributesGroups.add(this) }

fun hasDocument() = documentInputStream != null && documentInputStream!!.available() > 0
fun hasDocument() = documentInputStream != null

val attributesCharset: javaCharset
get() = operationGroup.getValue("attributes-charset")
Expand Down Expand Up @@ -165,14 +164,13 @@ abstract class IppMessage() {
// ------------------------

private fun copyUnconsumedDocumentInputStream(outputStream: OutputStream) {
if (hasDocument() && documentInputStreamIsConsumed) throw IppException("documentInputStream is consumed")
if (hasDocument() && documentInputStream!!.available() == 0) throw IppException("documentInputStream is consumed")
val outputStreamWithCopySupport =
if (keepDocumentCopy) ByteArraySavingOutputStream(outputStream)
else outputStream
documentInputStream!!
.copyTo(outputStreamWithCopySupport) // returns number of bytes copied
.apply { logger.finer { "Consumed documentInputStreamWithUncompressingSupport: $this bytes" } }
documentInputStreamIsConsumed = true
if (outputStreamWithCopySupport is ByteArraySavingOutputStream) {
documentBytes = outputStreamWithCopySupport.getSavedBytes()
logger.finer("Keeping ${documentBytes!!.size} document bytes")
Expand Down
10 changes: 7 additions & 3 deletions src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package de.gmuth.ipp.core
* Copyright (c) 2020-2024 Gerhard Muth
*/

import de.gmuth.log.Logging
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File.createTempFile
Expand All @@ -17,6 +18,9 @@ class IppMessageTests {

private val logger = getLogger(javaClass.name)

@BeforeTest
fun setUp() { Logging.configure() }

private val message = object : IppMessage() {
override val codeDescription: String
get() = "codeDescription"
Expand Down Expand Up @@ -56,10 +60,11 @@ class IppMessageTests {
} finally {
tmpFile.delete()
}
assertTrue(documentInputStreamIsConsumed)
assertEquals(38, rawBytes!!.size)
assertFailsWith<IppException> {
write(ByteArrayOutputStream())
}.apply {
logger.info(toString())
}
toString() // cover toString
log(logger) // cover log
Expand All @@ -79,8 +84,8 @@ class IppMessageTests {
val tmpFile2 = createTempFile("test", null)
try {
IppMessage.keepDocumentCopy = true
write(tmpFile0.outputStream())
assertTrue(hasDocument())
write(tmpFile0.outputStream(), true)
saveDocumentBytes(tmpFile1)
assertEquals(26, tmpFile1.length())
val ippBytes = encode(appendDocumentIfAvailable = false) // trigger saving raw bytes
Expand All @@ -91,7 +96,6 @@ class IppMessageTests {
tmpFile1.delete()
tmpFile2.delete()
}
assertTrue(documentInputStreamIsConsumed)
}
}

Expand Down

0 comments on commit 5fd89e7

Please sign in to comment.