Refactor ByteArrayOutputStream into synchronized and non-synchronized versions#108
Conversation
aherbert
left a comment
There was a problem hiding this comment.
This looks like a nice conversion and maintains the previous support. I'd be wary of using a new dependency just to mark the code as Threadsafe with an annotation on an existing class. Bringing in an annotation framework is a different topic.
Using the name FastXXX is presumptuous but I cannot think of something better. NonSyncXXX is not as friendly. Others may have comments on this.
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.code.findbugs</groupId> | ||
| <artifactId>jsr305</artifactId> |
There was a problem hiding this comment.
Is this just to bring in the @javax.annotation.concurrent.ThreadSafe annotation? I don't think this necessary since you already document the thread safety in the javadoc. Nothing else in the code base uses this non-standard annotation support.
There was a problem hiding this comment.
Well yes it is to bring in the annotation... but these annotations can also be used by various tools like FindBugs and others to detect issues.
I think it adds value, and jsr305 is tiny (19KB). I can remove it if people prefer? It also adds useful annotations like @Nullable and @NotNull for similar purposes.
I can remove it though if that is preferred. Should I do that?
There was a problem hiding this comment.
I would remove it from this PR. Adding an annotation dependency should be in another PR that perhaps then makes more use of it than a single location. The Guava codebase uses these throughout and in that case they have value for a bug detection tool. I don't think a single annotation of @ThreadSafe is likely to detect many (any?) bugs given that synchronized code should be less of a problem than unsynchronized code. If you do have a bug detection tool that flags this class as a problem when used in a concurrent scenario a brief look at the javadoc (which notes this is safe for concurrent use) will allow you to mark the bug as false positive.
There was a problem hiding this comment.
Okie dokie. Done.
|
|
||
| private static Stream<Arguments> baosFactories() { | ||
| final BAOSFactory syncBaosFactory = size -> new ByteArrayOutputStream(size); | ||
| final BAOSFactory nonSyncBaos = size -> new FastByteArrayOutputStream(size); |
4db1a3f to
64fbcf7
Compare
I am a big -1 on calling a new class "Fast" since it is not fast, it just removes thread-safety. You can hope that it is "faster" than the standard class but that's it. The JRE has been calling some thread-safe classes
My preference is |
|
@garydgregory Okay I understand. Your preferred option is very long though. How do you feel about:
|
The least worst IMO.
Oh, the horror! ;-) There is no convention that equates "U" to "Unsynchronized", if anything "U" usually stands for "Unsigned".
Even worse IMO! Same comment as above except US usually means... United States?
Nope, same as for "Fast", unsafe compared in what area? Will this be a CVE? I do not think there should be an issue spelling out Unsynchronized, if you look around, you'll see similar names spelled out:
So, IMO, just say what it does. |
|
@garydgregory Fair points. I have renamed it as you suggested. |
| * like the original. The only exception is the deprecated | ||
| * {@link java.io.ByteArrayOutputStream#toString(int)} method that has been | ||
| * ignored. | ||
| */ |
| * <p> | ||
| * The data can be retrieved using <code>toByteArray()</code> and | ||
| * <code>toString()</code>. | ||
| * <p> |
| } | ||
|
|
||
| /** | ||
| * Write the bytes to byte array. |
| } | ||
|
|
||
| /** | ||
| * Return the current size of the byte array. |
| * using the platform default charset. | ||
| * @return the contents of the byte array as a String | ||
| * @see java.io.ByteArrayOutputStream#toString() | ||
| * @deprecated 2.5 use {@link #toString(String)} instead |
There was a problem hiding this comment.
I do not think it makes sense to deprecate toString() since it is such a basic JRE method that will never go away.
There was a problem hiding this comment.
This was how it was previously in ByteArrayOutputStream. I just left it as I found it!
| import java.io.OutputStream; | ||
|
|
||
| /** | ||
| * This class implements a version of {@link AbstractByteArrayOutputStream} |
There was a problem hiding this comment.
"This class implements..." -> "Implements..."
|
@garydgregory I have incorporated your further feedback now. |
|
@garydgregory Adding the closing |
|
Looking at the line errors for your |
|
@aherbert Good point. I have incorporated the changes as you suggested. |
|
Hi @adamretter |
|
@garydgregory Hmm, from what I read in coveralls.io the code coverage after this PR is |
Please run the command I provided and look at the JaCoCo report. You will see that the new class has less coverage than the current (refactored) one. We should strive to give the best possible code coverage for changes and new code. |
|
@garydgregory Sure I see it now. I have just pushed a commit to further improve coverage. |
|
@garydgregory @aherbert Yay! Thanks :-) |
unsynchronized versions #108. Simplify method name.
… versions (apache#108) * Split ByteArrayOutputStream into synchronized and non-synchronized versions * Improve the test coverage of AbstractByteArrayOutputStream and sub-classes * Address review comments by @aherbert * Address review comments by @garydgregory * Address further review comments by @garydgregory * Remove </p> tags, breaks the javadoc build * Address review comments by @aherbert * Improve coverage of tests
unsynchronized versions apache#108. Simplify method name.
This abstracts the functionality of
ByteArrayOutputStreamintoAbstractByteArrayOutputStream. Synchronized and non-synchronized (for performance) implementations are then provided inByteArrayOutputStreamandFastByteArrayOutputStream.Tests are also provided.
p.s. I have just submitted by ICLA by email.