Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-4929

"read(byte[],int,int)" should be overridden

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      * Provide an override of "read(byte[],int,int)" for this class.
    • Highlighting:
      Hide

      Class name declaration

      Show
      Class name declaration
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Irrelevant for Languages:
      ABAP, APEX, C#, C, C++, Cobol, CSS, Flex, Go, HTML, JavaScript, Kotlin, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Ruby, Rust, Scala, Solidity, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      30min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources

      Description

      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

      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

      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

      This rule doesn't raise an issue when the class is declared abstract.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                alexandre.gigleux Alexandre Gigleux
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: