diff --git a/src/main/kotlin/ipp/printJobByteArrayVersion.kt b/src/main/kotlin/ipp/printJobByteArrayVersion.kt index 0453445..bf2e578 100644 --- a/src/main/kotlin/ipp/printJobByteArrayVersion.kt +++ b/src/main/kotlin/ipp/printJobByteArrayVersion.kt @@ -8,6 +8,7 @@ package ipp import java.io.* import java.net.HttpURLConnection import java.net.URI +import java.nio.charset.Charset fun main(args: Array) { val (printerURI, documentInputStream) = getArgsOrThrowUsage(args) @@ -15,7 +16,7 @@ fun main(args: Array) { } fun printJobByteArrayVersion(uri: URI, documentInputStream: InputStream) { - val charset = Charsets.UTF_8 + var charset = Charsets.US_ASCII // encode Print-Job operation val byteArrayOutputStream = ByteArrayOutputStream() @@ -91,6 +92,7 @@ fun printJobByteArrayVersion(uri: URI, documentInputStream: InputStream) { } } println(String.format(" %s (%02X) = %s", name, tag, value)) + if (name == "attributes-charset") charset = Charset.forName(value as String) } while (tag != 0x03.toByte()) } } \ No newline at end of file diff --git a/src/main/kotlin/ipp/printJobStreamingVersion.kt b/src/main/kotlin/ipp/printJobStreamingVersion.kt index 04688c1..20eb61e 100644 --- a/src/main/kotlin/ipp/printJobStreamingVersion.kt +++ b/src/main/kotlin/ipp/printJobStreamingVersion.kt @@ -11,6 +11,7 @@ import java.io.IOException import java.io.InputStream import java.net.HttpURLConnection import java.net.URI +import java.nio.charset.Charset fun main(args: Array) { val (printerURI, documentInputStream) = getArgsOrThrowUsage(args) @@ -18,76 +19,76 @@ fun main(args: Array) { } fun printJobStreamingVersion(uri: URI, documentInputStream: InputStream) { - val charset = Charsets.UTF_8 + var charset = Charsets.US_ASCII + val ippContentType = "application/ipp" println("send ipp request to $uri") - with(uri.toURL().openConnection() as HttpURLConnection) { - val ippContentType = "application/ipp" + val httpURLConnection = uri.toURL().openConnection() as HttpURLConnection + with(httpURLConnection) { setConnectTimeout(3000) setDoOutput(true) setRequestProperty("Content-Type", ippContentType) - - // encode ipp request 'Print-Job operation' - with(DataOutputStream(outputStream)) { - fun writeAttribute(tag: Int, name: String, value: String) { - writeByte(tag) - writeShort(name.length) - write(name.toByteArray(charset)) - writeShort(value.length) - write(value.toByteArray(charset)) - } - writeShort(0x0101) // ipp version - writeShort(0x0002) // print job operation - writeInt(0x002A) // request id - writeByte(0x01) // operation group tag - writeAttribute(0x47, "attributes-charset", charset.name().toLowerCase()) - writeAttribute(0x48, "attributes-natural-language", "en") - writeAttribute(0x45, "printer-uri", "$uri") - writeByte(0x03) // end tag - // append document - documentInputStream.copyTo(outputStream) - close() - outputStream.close() + } + // encode ipp request 'Print-Job operation' + with(DataOutputStream(httpURLConnection.outputStream)) { + fun writeAttribute(tag: Int, name: String, value: String) { + writeByte(tag) + writeShort(name.length) + write(name.toByteArray(charset)) + writeShort(value.length) + write(value.toByteArray(charset)) } - - // check http response + writeShort(0x0101) // ipp version + writeShort(0x0002) // print job operation + writeInt(0x002A) // request id + writeByte(0x01) // operation group tag + writeAttribute(0x47, "attributes-charset", charset.name().toLowerCase()) + writeAttribute(0x48, "attributes-natural-language", "en") + writeAttribute(0x45, "printer-uri", "$uri") + writeByte(0x03) // end tag + // append document + documentInputStream.copyTo(httpURLConnection.outputStream) + close() + } + // check http response + with(httpURLConnection) { if (getHeaderField("Content-Type") != ippContentType) { throw IOException("response from $uri is not '$ippContentType'") } if (responseCode != 200) { throw IOException("post to $uri failed with http status $responseCode") } - - // decode ipp response - with(DataInputStream(inputStream)) { - fun readValue(): ByteArray = readNBytes(readShort().toInt()) - println(String.format("ipp version %d.%s", readByte(), readByte())) - println(String.format("ipp status %04X", readShort())) - readInt() // ignore request id - var tag: Byte - do { - tag = readByte() - if (tag < 0x10) { - println(String.format("group %02X", tag)) - continue + } + // decode ipp response + with(DataInputStream(httpURLConnection.inputStream)) { + fun readValue(): ByteArray = readNBytes(readShort().toInt()) + println(String.format("ipp version %d.%s", readByte(), readByte())) + println(String.format("ipp status %04X", readShort())) + readInt() // ignore request id + var tag: Byte + do { + tag = readByte() + if (tag < 0x10) { + println(String.format("group %02X", tag)) + continue + } + // attribute tag + val name = String(readValue(), charset) + val value: Any = when (tag.toInt()) { + 0x21, 0x23 -> { + readShort() + readInt() } - // attribute tag - val name = String(readValue(), charset) - val value: Any = when (tag.toInt()) { - 0x21, 0x23 -> { - readShort() - readInt() - } - 0x41, 0x44, 0x45, 0x47, 0x48 -> { - String(readValue(), charset) - } - else -> { - readValue() - String.format("", tag) - } + 0x41, 0x44, 0x45, 0x47, 0x48 -> { + String(readValue(), charset) } - println(String.format(" %s (%02X) = %s", name, tag, value)) - } while (tag != 0x03.toByte()) - } + else -> { + readValue() + String.format("", tag) + } + } + println(String.format(" %s (%02X) = %s", name, tag, value)) + if (name == "attributes-charset") charset = Charset.forName(value as String) + } while (tag != 0x03.toByte()) } } \ No newline at end of file