mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-10 10:49:39 +02:00
Kotlin conversion: CRLFGeneratorStream
This commit is contained in:
parent
068aa0ec27
commit
a50be47fa4
2 changed files with 52 additions and 55 deletions
|
@ -0,0 +1,52 @@
|
|||
// 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 org.pgpainless.algorithm.StreamEncoding
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* [OutputStream] which applies CR-LF encoding of its input data, based on the desired [StreamEncoding].
|
||||
* This implementation originates from the Bouncy Castle library.
|
||||
*/
|
||||
class CRLFGeneratorStream(
|
||||
private val crlfOut: OutputStream,
|
||||
encoding: StreamEncoding
|
||||
) : OutputStream() {
|
||||
|
||||
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
|
||||
crlfOut.write('\r'.code)
|
||||
} else if (lastB == '\r'.code) { // MAC
|
||||
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()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue