Skip to content

Commit

Permalink
put it in the enum java.io
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Sep 27, 2024
1 parent 493680b commit d48cb5e
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 287 deletions.
276 changes: 139 additions & 137 deletions Sources/JavApi/io/BufferedOutputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,155 +32,157 @@
*
* see ``java.io.BufferedInputStream``
*/
/// - Since: JavaApi > 0.18.0 (Java 1.0)
open class BufferedOutputStream : FilterOutputStream {
/**
* The buffer containing the bytes to be written to the target stream.
*/
extension java.io {
/// - Since: JavaApi > 0.18.0 (Java 1.0)
public var buf : [UInt8]

/**
* The total number of bytes inside the byte array {@code buf}.
*/
/// - Since: JavaApi > 0.18.0 (Java 1.0)
public var count : Int = 0

/**
* Constructs a new {@code BufferedOutputStream} on the {@link OutputStream}
* {@code out}. The buffer size is set to the default value of 8 KB.
*
* @param out
* the {@code OutputStream} for which write operations are
* buffered.
*/
/// - Since: JavaApi > 0.18.0 (Java 1.0)
public override init(_ out : java.io.OutputStream) {
buf = Array(Array(repeating: 0, count: 8192))
super.init(out)
}

/**
* Constructs a new {@code BufferedOutputStream} on the {@link OutputStream}
* {@code out}. The buffer size is set to {@code size}.
*
* @param out
* the output stream for which write operations are buffered.
* @param size
* the size of the buffer in bytes.
* @throws IllegalArgumentException
* if {@code size <= 0}.
*/
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public init(_ out : java.io.OutputStream, _ size : Int) throws {
if (size <= 0) {
throw java.lang.Throwable.IllegalArgumentException("size must be > 0")
open class BufferedOutputStream : FilterOutputStream {
/**
* The buffer containing the bytes to be written to the target stream.
*/
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public var buf : [UInt8]

/**
* The total number of bytes inside the byte array {@code buf}.
*/
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public var count : Int = 0

/**
* Constructs a new {@code BufferedOutputStream} on the {@link OutputStream}
* {@code out}. The buffer size is set to the default value of 8 KB.
*
* @param out
* the {@code OutputStream} for which write operations are
* buffered.
*/
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public override init(_ out : java.io.OutputStream) {
buf = Array(Array(repeating: 0, count: 8192))
super.init(out)
}

/**
* Constructs a new {@code BufferedOutputStream} on the {@link OutputStream}
* {@code out}. The buffer size is set to {@code size}.
*
* @param out
* the output stream for which write operations are buffered.
* @param size
* the size of the buffer in bytes.
* @throws IllegalArgumentException
* if {@code size <= 0}.
*/
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public init(_ out : java.io.OutputStream, _ size : Int) throws {
if (size <= 0) {
throw java.lang.Throwable.IllegalArgumentException("size must be > 0")
}
buf = Array(repeating: 0, count: size)
super.init(out);
}
buf = Array(repeating: 0, count: size)
super.init(out);
}

/**
* Flushes this stream to ensure all pending data is written out to the
* target stream. In addition, the target stream is flushed.
*
* @throws IOException
* if an error occurs attempting to flush this stream.
*/
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public override func flush() throws {
try flushInternal();
try out.flush();
}

/**
* Writes {@code count} bytes from the byte array {@code buffer} starting at
* {@code offset} to this stream. If there is room in the buffer to hold the
* bytes, they are copied in. If not, the buffered bytes plus the bytes in
* {@code buffer} are written to the target stream, the target is flushed,
* and the buffer is cleared.
*
* @param buffer
* the buffer to be written.
* @param offset
* the start position in {@code buffer} from where to get bytes.
* @param length
* the number of bytes from {@code buffer} to write to this
* stream.
* @throws IndexOutOfBoundsException
* if {@code offset < 0} or {@code length < 0}, or if
* {@code offset + length} is greater than the size of
* {@code buffer}.
* @throws IOException
* if an error occurs attempting to write to this stream.
* @throws NullPointerException
* if {@code buffer} is {@code null}.
* @throws ArrayIndexOutOfBoundsException
* If offset or count is outside of bounds.
*/
public override func write(_ buffer : [UInt8], _ offset : Int, _ length : Int) throws {
var internalBuffer = buf;

if (length >= internalBuffer.length) {
/**
* Flushes this stream to ensure all pending data is written out to the
* target stream. In addition, the target stream is flushed.
*
* @throws IOException
* if an error occurs attempting to flush this stream.
*/
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public override func flush() throws {
try flushInternal();
try out.write(buffer, offset, length)
return
try out.flush();
}

if (offset < 0 || offset > buffer.length - length) {
// luni.12=Offset out of bounds \: {0}
throw java.lang.Throwable.ArrayIndexOutOfBoundsException(offset, "Offset out of bounds : \(offset)")
/**
* Writes {@code count} bytes from the byte array {@code buffer} starting at
* {@code offset} to this stream. If there is room in the buffer to hold the
* bytes, they are copied in. If not, the buffered bytes plus the bytes in
* {@code buffer} are written to the target stream, the target is flushed,
* and the buffer is cleared.
*
* @param buffer
* the buffer to be written.
* @param offset
* the start position in {@code buffer} from where to get bytes.
* @param length
* the number of bytes from {@code buffer} to write to this
* stream.
* @throws IndexOutOfBoundsException
* if {@code offset < 0} or {@code length < 0}, or if
* {@code offset + length} is greater than the size of
* {@code buffer}.
* @throws IOException
* if an error occurs attempting to write to this stream.
* @throws NullPointerException
* if {@code buffer} is {@code null}.
* @throws ArrayIndexOutOfBoundsException
* If offset or count is outside of bounds.
*/
public override func write(_ buffer : [UInt8], _ offset : Int, _ length : Int) throws {
var internalBuffer = buf;

}
if (length < 0) {
// luni.18=Length out of bounds \: {0}
throw java.lang.Throwable.ArrayIndexOutOfBoundsException(length, "Length out of bounds : \(length)")
if (length >= internalBuffer.length) {
try flushInternal();
try out.write(buffer, offset, length)
return
}

if (offset < 0 || offset > buffer.length - length) {
// luni.12=Offset out of bounds \: {0}
throw java.lang.Throwable.ArrayIndexOutOfBoundsException(offset, "Offset out of bounds : \(offset)")

}
if (length < 0) {
// luni.18=Length out of bounds \: {0}
throw java.lang.Throwable.ArrayIndexOutOfBoundsException(length, "Length out of bounds : \(length)")
}

// flush the internal buffer first if we have not enough space left
if (length >= (internalBuffer.length - count)) {
try flushInternal();
}

// the length is always less than (internalBuffer.length - count) here so arraycopy is safe
System.arraycopy(buffer, offset, &internalBuffer, count, length);
count += length;
}

// flush the internal buffer first if we have not enough space left
if (length >= (internalBuffer.length - count)) {
try flushInternal();
public override func close() throws {
try super.close();
}

// the length is always less than (internalBuffer.length - count) here so arraycopy is safe
System.arraycopy(buffer, offset, &internalBuffer, count, length);
count += length;
}

public override func close() throws {
try super.close();
}

/**
* Writes one byte to this stream. Only the low order byte of the integer
* {@code oneByte} is written. If there is room in the buffer, the byte is
* copied into the buffer and the count incremented. Otherwise, the buffer
* plus {@code oneByte} are written to the target stream, the target is
* flushed, and the buffer is reset.
*
* @param oneByte
* the byte to be written.
* @throws IOException
* if an error occurs attempting to write to this stream.
*/
public override func write(_ oneByte : Int) throws {
var internalBuffer = buf;

if (count == internalBuffer.length) {
try out.write(internalBuffer, 0, count);
count = 0;
/**
* Writes one byte to this stream. Only the low order byte of the integer
* {@code oneByte} is written. If there is room in the buffer, the byte is
* copied into the buffer and the count incremented. Otherwise, the buffer
* plus {@code oneByte} are written to the target stream, the target is
* flushed, and the buffer is reset.
*
* @param oneByte
* the byte to be written.
* @throws IOException
* if an error occurs attempting to write to this stream.
*/
public override func write(_ oneByte : Int) throws {
var internalBuffer = buf;

if (count == internalBuffer.length) {
try out.write(internalBuffer, 0, count);
count = 0;
}
internalBuffer[count] = UInt8 (oneByte)
count += 1
}
internalBuffer[count] = UInt8 (oneByte)
count += 1
}

/**
* Flushes only internal buffer.
*/
private func flushInternal() throws {
if (count > 0) {
try out.write(buf, 0, count);
count = 0;

/**
* Flushes only internal buffer.
*/
private func flushInternal() throws {
if (count > 0) {
try out.write(buf, 0, count);
count = 0;
}
}
}
}
39 changes: 20 additions & 19 deletions Sources/JavApi/io/DataInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
* SPDX-FileCopyrightText: 2024 - Sebastian Ritter <bastie@users.noreply.github.com>
* SPDX-License-Identifier: MIT
*/

/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public protocol DataInput {

func readBoolean() throws -> Bool
func readByte() throws -> Int8
func readChar() throws -> Character
func readDouble() throws -> Double
func readFloat() throws -> Float
func readFully(_ buffer: inout [UInt8]) throws
func readFully(_ buffer: inout [UInt8], _ offset : Int, _ length : Int) throws
func readInt() throws -> Int
func readLine() throws -> String
func readLong() throws -> Int64
func readShort() throws -> Int16
func readUnsignedByte() throws -> Int
func readUnsignedShort() throws -> Int
func readUTF() throws -> String
func skipBytes(_ n: Int) throws
extension java.io {
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public protocol DataInput {

func readBoolean() throws -> Bool
func readByte() throws -> Int8
func readChar() throws -> Character
func readDouble() throws -> Double
func readFloat() throws -> Float
func readFully(_ buffer: inout [UInt8]) throws
func readFully(_ buffer: inout [UInt8], _ offset : Int, _ length : Int) throws
func readInt() throws -> Int
func readLine() throws -> String
func readLong() throws -> Int64
func readShort() throws -> Int16
func readUnsignedByte() throws -> Int
func readUnsignedShort() throws -> Int
func readUTF() throws -> String
func skipBytes(_ n: Int) throws
}
}
8 changes: 5 additions & 3 deletions Sources/JavApi/io/DataOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* SPDX-License-Identifier: MIT
*/

/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public protocol DataOutput {

extension java.io {
/// - Since: JavaApi &gt; 0.18.0 (Java 1.0)
public protocol DataOutput {

}
}
Loading

0 comments on commit d48cb5e

Please sign in to comment.