Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:59:01+00:00 2026-06-07T20:59:01+00:00

I was going through an open source project where they were creating an output

  • 0

I was going through an open source project where they were creating an output stream, and came across the following method:

@Override public void write(byte[] buffer, int offset, int length) {
    if (buffer == null) {
        throw new NullPointerException("buffer is null");
    }
    if (buffer.length < 0) { // NOTE HERE
        throw new IllegalArgumentException("buffer length < 0");
    }
    if (offset < 0) {
        throw new IndexOutOfBoundsException(String.format("offset %d < 0", offset));
    }
    if (length < 0) {
        throw new IndexOutOfBoundsException(String.format("length %d < 0", length));
    }
    if (offset > buffer.length || length > buffer.length - offset) {
        throw new IndexOutOfBoundsException(String.format("offset %d + length %d > buffer"                                                       " length %d", offset, length, buffer.length));
    }
}

So the byte[] buffer is just a normal old byte[]. We know it’s not null. Is it even possible to make it have a length of less than 0? Like, could it be done with reflection and that’s what they’re guarding against?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:59:03+00:00Added an answer on June 7, 2026 at 8:59 pm

    No, this can never happen. The length is guaranteed to be non-negative as per the Java specifications.

    The members of an array type are all of the following:

    • The public final field length, which contains the number of components
      of the array. length may be positive or zero.

    Source: JLS §10.7

    As mprivat mentioned, if you ever try to create an array of negative size, a NegativeArraySizeException will be thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been going through some open-source code for a Twitter app, and came across
While going through Effective C++ (by Scott Meyers), I came across the following code
Recently, I was going through an open-source project and although I have been developing
Going through some documentation on modifying CGImageRef data, I came across a strange example
I am a python django newbie, and was going through some open source code,
I'm reading through a open source Rails project code and I see SQL statements
I was going through OSMF (Open Source Media Framework). I wonder why it's called
Going through Javascript documentation, I found the following two functions on a Javascript object
Going through the microsoft authentication tutorial listed here they have you create a master
There are few open source projects/APIs/libraries that we use in our project ( Spring,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.