Solved

Java - Printing buffer data together

Posted on 2016-07-15
4
48 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 27

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 27

Accepted Solution

by:
dpearson earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 56
Adding multiple JVM environments to RedHat 6 7 69
Why is enum singleton a better approach than static factory 3 48
Java program running SQL query 5 48
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 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