Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java - Printing buffer data together

Posted on 2016-07-15
4
Medium Priority
?
59 Views
Last Modified: 2016-07-17
Hello there,

I have this piece of code and want to print out the data in the buffer,cam somebody please guide me how can i achieve it.

cheers
Zolf

 Buffer buf = Buffer.buffer();
            buf.appendByte((byte) 0x02).appendString("CLR").appendByte((byte) 0x03)
                    .appendByte((byte) 0x1b).appendString("K1").appendByte((byte) 0x0d)
                    .appendByte((byte) 0x1b).appendString("C").appendByte((byte) 0x0d)
                    .appendByte((byte) 0x1b).appendString("K2").appendByte((byte) 0x0d)
                    .appendByte((byte) 0x1b).appendString("C").appendByte((byte) 0x0d)
                    .appendByte((byte) 0x1b).appendString("K3").appendByte((byte) 0x0d)
                    .appendByte((byte) 0x1b).appendString("C").appendByte((byte) 0x0d)
                    .appendByte((byte) 0x1b).appendString("K4").appendByte((byte) 0x0d)
                    .appendByte((byte) 0x1b).appendString("C").appendByte((byte) 0x0d);
                    socket.write(buf);

Open in new window

0
Comment
Question by:zolf
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:dpearson
ID: 41714725
I don't think Buffer here is the standard Java buffer class - i.e. java.nio.Buffer

I suspect this is an internal class - in which case you'd need to look inside that class to see how it is representing the data and how to print it out.

If it contains a ByteBuffer then that has a getChar() method which you can use to read and print the current contents of the buffer.

Doug
0
 

Author Comment

by:zolf
ID: 41715035
thanks for your comments.

Yes I am using a 3rd party buffer class and the link to it is http://vertx.io/docs/apidocs/io/vertx/core/buffer/Buffer.html
0
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 41715037
Given that I would suggest working with one of these methods:

String      getString(int start, int end)
Returns a copy of a sub-sequence the Buffer as a String starting at position start and ending at position end - 1 interpreted as a String in UTF-8 encoding

Or if that doesn't work try this one:

byte[]      getBytes(int start, int end)
Returns a copy of a sub-sequence the Buffer as a byte[] starting at position start and ending at position end - 1

In both cases, end would be:
int      length()
Returns the length of the buffer, measured in bytes.

That should give you access to the data in the buffer.

If length returns 0, then you should try call this before the socket.write(buf) which may empty the buffer, depending how it is written.

Hope that helps,

Doug
0
 

Author Closing Comment

by:zolf
ID: 41715065
cheers
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
A solution for Fortify Path Manipulation.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question