pgpainless/pgpainless-core/src/main/kotlin/org/pgpainless/encryption_signing/CRLFGeneratorStream.kt

51 lines
1.3 KiB
Kotlin
Raw Normal View History

2023-09-20 13:57:16 +02:00
// SPDX-FileCopyrightText: 2021 David Hook <dgh@cryptoworkshop.com>
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.encryption_signing
import java.io.OutputStream
import org.pgpainless.algorithm.StreamEncoding
2023-09-20 13:57:16 +02:00
/**
* [OutputStream] which applies CR-LF encoding of its input data, based on the desired
* [StreamEncoding]. This implementation originates from the Bouncy Castle library.
2023-09-20 13:57:16 +02:00
*/
class CRLFGeneratorStream(private val crlfOut: OutputStream, encoding: StreamEncoding) :
OutputStream() {
2023-09-20 13:57:16 +02:00
private val isBinary: Boolean
private var lastB = 0
init {
isBinary = encoding == StreamEncoding.BINARY
}
override fun write(b: Int) {
if (!isBinary) {
if (b == '\n'.code && lastB != '\r'.code) { // Unix
2023-09-20 13:57:16 +02:00
crlfOut.write('\r'.code)
} else if (lastB == '\r'.code) { // MAC
2023-09-20 13:57:16 +02:00
if (b != '\n'.code) {
crlfOut.write('\n'.code)
}
}
lastB = b
}
crlfOut.write(b)
}
override fun close() {
if (!isBinary && lastB == 'r'.code) {
crlfOut.write('\n'.code)
}
crlfOut.close()
}
override fun flush() {
super.flush()
crlfOut.flush()
}
}