rspec/rules/S4929/java/rule.adoc

80 lines
1.9 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
When directly subclassing ``++java.io.InputStream++`` or ``++java.io.FilterInputStream++``, the only requirement is that you implement the method ``++read()++``. However most uses for such streams don't read a single byte at a time and the default implementation for ``++read(byte[],int,int)++`` will call ``++read(int)++`` for every single byte in the array which can create a lot of overhead and is utterly inefficient. It is therefore strongly recommended that subclasses provide an efficient implementation of ``++read(byte[],int,int)++``.
This rule raises an issue when a direct subclass of ``++java.io.InputStream++`` or ``++java.io.FilterInputStream++`` doesn't provide an override of ``++read(byte[],int,int)++``.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public class MyInputStream extends java.io.InputStream {
private FileInputStream fin;
public MyInputStream(File file) throws IOException {
fin = new FileInputStream(file);
}
@Override
public int read() throws IOException {
return fin.read();
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public class MyInputStream extends java.io.InputStream {
private FileInputStream fin;
public MyInputStream(File file) throws IOException {
fin = new FileInputStream(file);
}
@Override
public int read() throws IOException {
return fin.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return fin.read(b, off, len);
}
}
----
=== Exceptions
2021-04-28 16:49:39 +02:00
This rule doesn't raise an issue when the class is declared ``++abstract++``.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Provide an override of "read(byte[],int,int)" for this class.
=== Highlighting
Class name declaration
'''
== Comments And Links
(visible only on this page)
=== relates to: S4349
endif::env-github,rspecator-view[]