Skip to content

Commit

Permalink
moved attribute compression to IppMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Dec 12, 2024
1 parent 389e4d0 commit 57e7253
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
20 changes: 6 additions & 14 deletions src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ package de.gmuth.ipp.core
* Copyright (c) 2020-2024 Gerhard Muth
*/

import de.gmuth.ipp.attributes.Compression
import de.gmuth.ipp.core.IppTag.*
import java.io.*
import java.util.logging.Level
import java.util.logging.Level.INFO
import java.util.logging.Logger
import java.util.logging.Logger.getLogger
import java.util.zip.DeflaterOutputStream
import java.util.zip.GZIPOutputStream
import java.nio.charset.Charset as javaCharset

abstract class IppMessage() {
Expand Down Expand Up @@ -88,6 +87,9 @@ abstract class IppMessage() {
val requestingUserName: String
get() = operationGroup.getValueAsString("requesting-user-name")

val compression: Compression
get() = Compression.fromString(operationGroup.getValue("compression"))

// --------
// ENCODING
// --------
Expand Down Expand Up @@ -154,7 +156,7 @@ abstract class IppMessage() {
// write documentBytes? take care of compression!
} else {
val outputStream = if (operationGroup.containsKey("compression")) {
getCompressingOutputStream(notCompressingOutputStream)
compression.getCompressingOutputStream(notCompressingOutputStream)
} else {
notCompressingOutputStream
}
Expand All @@ -164,16 +166,6 @@ abstract class IppMessage() {
}
}

private fun getCompressingOutputStream(uncompressedOutputStream: OutputStream) =
with(operationGroup.getValueAsString("compression")) {
when (this) {
"none" -> uncompressedOutputStream
"gzip" -> GZIPOutputStream(uncompressedOutputStream)
"deflate" -> DeflaterOutputStream(uncompressedOutputStream)
else -> throw NotImplementedError("compression '$this'")
}
}

private fun copyUnconsumedDocumentInputStream(outputStream: OutputStream): Long {
if (hasDocument() && documentInputStreamIsConsumed) {
throw IppException("documentInputStream is consumed")
Expand All @@ -194,7 +186,7 @@ abstract class IppMessage() {

fun saveDocumentBytes(file: File) = file.run {
if (documentBytes == null || documentBytes!!.isEmpty()) throw IppException("No documentBytes available. You should enable flag IppMessage.keepDocumentCopy.")
outputStream().use { ByteArrayInputStream(documentBytes).copyTo(it)}
outputStream().use { ByteArrayInputStream(documentBytes).copyTo(it) }
logger.info { "Saved ${length()} document bytes to $path" }
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/de/gmuth/ipp/core/IppRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class IppRequest : IppMessage {
val requestedAttributes: List<String>
get() = operationGroup.getValues("requested-attributes")

val compression: Compression
get() = Compression.fromString(operationGroup.getValueAsString("compression"))

constructor() : super()

constructor(
Expand Down

0 comments on commit 57e7253

Please sign in to comment.