-
Notifications
You must be signed in to change notification settings - Fork 396
Fix #5014: Implement java.io.BufferedWriter #5015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Implementation copied from Scala-native
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package java.io | ||
|
||
class BufferedWriter(out: Writer, sz: Int) extends Writer { | ||
|
||
if (sz <= 0) throw new IllegalArgumentException("Buffer size <= 0") | ||
|
||
def this(out: Writer) = this(out, 4096) | ||
|
||
private val buffer: Array[Char] = new Array[Char](sz) | ||
private var pos: Int = 0 | ||
private var closed: Boolean = false | ||
|
||
def close(): Unit = if (!closed) { | ||
flush() | ||
out.close() | ||
closed = true | ||
} | ||
|
||
def flush(): Unit = { | ||
ensureOpen() | ||
out.write(buffer, 0, pos) | ||
out.flush() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot infer this behavior from the JDK documentation. Maybe we should add an explicit test to ensure the behavior is aligned? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I read
|
||
pos = 0 | ||
} | ||
|
||
def newLine(): Unit = | ||
write(System.lineSeparator(), 0, System.lineSeparator().length) | ||
ThijsBroersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override def write(c: Int): Unit = | ||
write(Array(c.toChar), 0, 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC there is always at least one spot free in the buffer (otherwise it would be flushed). So couldn't this be: buffer(pos) = c.toChar
pos += 1
if (pos == len)
flush() |
||
|
||
override def write(s: String, off: Int, len: Int): Unit = | ||
ThijsBroersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
write(s.toCharArray, off, len) | ||
|
||
def write(cbuf: Array[Char], off: Int, len: Int): Unit = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the JDK doc:
IIUC this behavior isn't implemented. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reimplemented the method |
||
ensureOpen() | ||
val available = sz - pos | ||
if (available >= len) { | ||
System.arraycopy(cbuf, off, buffer, pos, len) | ||
pos += len | ||
if (pos == sz) flush() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will also flush the underlying stream. Are you sure this is the right behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also (IIRC) Scala.js style guide dictates that side-effecting calls are on their own line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the |
||
} else { | ||
write(cbuf, off, available) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of calling the method recursively, have you considered a helper:
(probably necessary anyways to build the "flush the buffer and write directly" behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it needs recursion. Reimplemented it to where it write to the buffer or it flushes the buffer and writes the all to the chained writer (as this is the behaviour I interpret from the JDK spec).
|
||
write(cbuf, off + available, len - available) | ||
} | ||
} | ||
|
||
private def ensureOpen(): Unit = | ||
if (closed) throw new IOException("Stream closed") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.scalajs.testsuite.javalib.io | ||
|
||
import java.io._ | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
import org.scalajs.testsuite.utils.AssertThrows.assertThrows | ||
|
||
class BufferedWriterTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll probably have to add more tests, notably (probably not a complete list, sorry):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added more test cases. |
||
|
||
@Test def creatingBufferedWriterWithBufferSizeZeroThrowsException(): Unit = { | ||
val writer = new OutputStreamWriter(new ByteArrayOutputStream) | ||
assertThrows( | ||
classOf[IllegalArgumentException], | ||
new BufferedWriter(writer, -1) | ||
) | ||
} | ||
|
||
@Test def canWriteSmallChunksToBufferedWriter(): Unit = { | ||
val stream = new ByteArrayOutputStream | ||
val writer = new BufferedWriter(new OutputStreamWriter(stream)) | ||
ThijsBroersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val string = "Hello, world!" | ||
writer.write(string) | ||
writer.flush() | ||
assertTrue(stream.toString == string) | ||
} | ||
|
||
@Test def canWriteChunkLargerThanBufferSizeToBufferedWriter(): Unit = { | ||
val stream = new ByteArrayOutputStream | ||
val writer = new BufferedWriter(new OutputStreamWriter(stream), 1) | ||
val string = "Hello, world!" | ||
writer.write(string) | ||
writer.flush() | ||
assertTrue(stream.toString == string) | ||
} | ||
|
||
@Test def closingTwiceIsHarmless(): Unit = { | ||
val stream = new ByteArrayOutputStream | ||
val writer = new BufferedWriter(new OutputStreamWriter(stream)) | ||
writer.close() | ||
writer.close() | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.