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 7662361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:46:54+00:00 2026-05-31T13:46:54+00:00

I have a very small threaded application, which is collecting small chunks of data

  • 0

I have a very small threaded application, which is collecting small chunks of data in arrays (because it is sound data, and Java wants that to be an array) and trying to put it into an ArrayList for storage. All of that is effectively the front half of a producer/consumer pattern.

Problem: It doesn’t seem to work.

On the producer end, I have this code:

  public synchronized void run() {
// do a whole bunch of audio set-up

try {
  // more audio stuff

  while (true) {
    if (producing) {
      byte[] data = new byte[line.getBufferSize() ];
      numBytesRead = line.read(data, 0, data.length);
      System.out.println("Producer:  Size of dat[] is " + data.length);

      // Save this chunk of data.
      buffer.addData(data);
    }

This seems straightforward, aside from the audio stuff and bookkeeping.
In the buffer class, I have:

public class Buffer {
  ArrayList list ;
  public void addData(byte[] data) {
    list.addAll(Arrays.asList(data));
  }

This also seems straightforward.
Here is the problem: If my array is of length (say) 1024, and the elements are all there (which I’ve verified that they are) I would expect the size of the ArrayList to grow by 1024 every time I add data. It doesn’t. It grows by 1, as though I was making either an ArrayList of ArrayLists or an ArrayList of Arrays, rather than the Arraylist of elements I desire.

I suspect I’m going to have this problem on the flip side as well, where I might have an ArrayList of tens of thousands of bytes, and want to retrieve an array of the first 1024 elements.

I cannot help but think I’m missing something very simple. Can anyone shed light on why this is not working? (Or if there is some fundamentally better way to do what I’m trying to do?)

  • 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-05-31T13:46:55+00:00Added an answer on May 31, 2026 at 1:46 pm

    Arrays.asList() will not perform the conversion from byte to Byte, it will return a list containing 1 element; the byte[] you pass in.

    If your aim is to add Byte objects for every byte, you will have to do that yourself in a loop. Note that this will use much more memory than passing byte[]s however.

    Also note that it is not garuanteed that, even if the input stream has more than enough data left, that you will read data.length bytes every time (result of buffer sizes, concurency, etc.) so you run the risk of passing a bunch of 0 bytes at the end of your buffer if you read less bytes than you asked for.

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

Sidebar

Related Questions

I have a very small Java codebase that I would like to expose as
I have a very small amount of data (~200 bytes) that I retrieve from
i have a very small utilty app written in c# that works fine on
I have a small (500kb) swing applet that displays very simple/limited set of small
I have found that my HTML is, to be honest, very clunky. Small, simple
I have a very small activity that must show an image. If picture is
I have a very small header that contains the following code. The code compiles
Suppose I have a very small float a (for instance a=0.5 ) that enters
Java. I have a very small int array (4 elements). What's the fastest way
When you need to have very small objects, say that contains 2 float property,

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.