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

  • Home
  • SEARCH
  • 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 8793077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:04:37+00:00 2026-06-13T23:04:37+00:00

I’m pushing data from my Java application over a USB connection to an Arduino

  • 0

I’m pushing data from my Java application over a USB connection to an Arduino in the other end.

The Arduino is only capable of buffering 64 bytes of data at its end, so I have to limit how many bytes are sent in each ‘gulp’ from my Java app (excess bytes will be lost). When the Arduino code is prepared to receive more bytes, it will send a simple ping up the wire.

So, I’ve extended BufferedOutputStream in a class ArduinoBufferedOutputStream, which wrappes the actual output stream. From different parts of the Java app an arbitrary number of bytes are written to the stream (with write(byte b)), and the stream is occationally flushed.

What I need (I guess) is to override BufferedOutputStreams flush method, so that it will not send more than 64 bytes, before receiving the ping from Arduino, at which time the stream should send 64 more bytes (or less).

     static class ArduinoBufferedOutputStream extends BufferedOutputStream {

        public static final int WIRE_CAPACITY = 25;
        private byte[] waiting = new byte[0];
        private int onWire = 0;

        public ArduinoBufferedOutputStream(final OutputStream wrapped) throws IOException {
            super(wrapped, 500);
        }

        public void ping() {
            this.onWire = 0;
            this.flush();
        }

        @Override
        public void flush() throws IOException {
            if (this.onWire >= WIRE_CAPACITY) {
                return; // we're exceeding capacity, don't to anything before the next ping
            }
            if (this.count > WIRE_CAPACITY) {
                this.waiting = new byte[this.count - WIRE_CAPACITY];
                System.arraycopy(this.buf, WIRE_CAPACITY, waiting, 0, this.count - WIRE_CAPACITY);
                this.buf = Arrays.copyOfRange(this.buf, 0, WIRE_CAPACITY);
                this.count = WIRE_CAPACITY;
            } else {
                this.waiting = new byte[0];
            }
            onWire += this.count;
            super.flush();
            if (this.waiting.length > 0) {
                System.arraycopy(this.waiting, 0, this.buf, 0, Math.min(this.waiting.length, WIRE_CAPACITY));
                this.count = Math.min(this.waiting.length, WIRE_CAPACITY);
            }
        }
    }

However, this doesn’t work properly. Bytes are lost if the buffer contains more than WIRE_CAPACITY bytes, as demonstrated by the following testcase:

@Test
public void testStream() throws IOException {
    final ArduinoBufferedOutputStream stream = new ArduinoDisplayOutputStream.ArduinoBufferedOutputStream(System.out);
    stream.write("This is a very, very long string, which must be made even longer to demonstrate the problem".getBytes());
    stream.flush();
    stream.ping();
    stream.ping();
    stream.ping();
    stream.ping();
    stream.ping();
    stream.ping();
    stream.ping();
    stream.ping();
    stream.ping();
}

The following string is printed: This is a very, very long string, which must be ma, while I obviously would like the whole string to be printed.

Can anyone see what I’m doing wrong here?
Or even better, does anyone know of an existing library that does what I want?

  • 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-13T23:04:37+00:00Added an answer on June 13, 2026 at 11:04 pm

    OK, I think I got it right, finally. Extending BufferedOutputStream was a dead end. Instead, I use a separate buffer, like @ben-lawry suggested. And by using a ConcurrentLinkedQueue as that buffer, I can easily poll WIRE CAPACITY number of bytes on each flush().

    Not terribly efficient, one might say, but the stuff going on on the Java side will still be speedy compared to the Arduino processing.

    Here’s what I got:

       static class ArduinoBufferedOutputStream extends OutputStream {
    
            public static final int WIRE_CAPACITY = 25;
            private final ConcurrentLinkedQueue<Byte> buffer = new ConcurrentLinkedQueue<Byte>();
            private int onWire = 0;
            private OutputStream wrapped;
    
            public ArduinoBufferedOutputStream(final OutputStream wrapped) throws IOException {
                super();
                this.wrapped = wrapped;
            }
    
            @Override
            public void write(int i) throws IOException {
                this.buffer.add((byte) i);
            }
    
            @Override
            public void write(byte[] bytes) throws IOException {
                for (byte aByte : bytes) {
                    this.buffer.add(aByte);
                }
            }
    
            @Override
            public void write(byte[] bytes, int off, int len) throws IOException {
                for (int i = off; i < len; i++) {
                    this.buffer.add(bytes[i]);
                }
            }
    
            public void ping() throws IOException {
                synchronized (this.buffer) {
                    this.onWire = 0;
                }
                this.flush();
            }
    
            @Override
            public void flush() throws IOException {
                synchronized (this.buffer) {
                    for (; this.onWire < WIRE_CAPACITY && buffer.size() > 0; this.onWire++) {
                        wrapped.write(buffer.poll());
                    }
                }
            }
        }
    

    Thanks, @gpeche and @ben-lawry – it certainly helped getting a few more eyes on the problem.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I want to construct a data frame in an Rcpp function, but when I
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.