?
Solved

ByteBuffer read problem

Posted on 2002-05-29
10
Medium Priority
?
487 Views
Last Modified: 2012-06-27
Hi All
I am suing this method to convert from ByteBuffer to String after reading from a SocketChannel:

public String decode(ByteBuffer buffer) throws CharacterCodingException {        
        Charset charset = Charset.forName("us-ascii");
        CharsetDecoder decoder = charset.newDecoder();
        CharBuffer charBuffer = decoder.decode(buffer);
        String result = charBuffer.toString();
        return result;
    }

I recive the following exception :

java.nio.charset.MalformedInputException: Input length = 1
     at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
     at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:763)
     at tv.tvgate.unitTests.NioSocket.decode(NioSocket.java:173)
     at tv.tvgate.unitTests.NioSocket.readMessage(NioSocket.java:156)
     at tv.tvgate.unitTests.NioSocket.processSelectionKey(NioSocket.java:115)
     at tv.tvgate.unitTests.NioSocket.<init>(NioSocket.java:81)
     at tv.tvgate.unitTests.NioSocket.main(NioSocket.java:195)

If I catch the MalformedInputException in this method and print the cause message I get :

java.lang.NullPointerException
     at tv.tvgate.unitTests.NioSocket.decode(NioSocket.java:178)
     at tv.tvgate.unitTests.NioSocket.readMessage(NioSocket.java:156)
     at tv.tvgate.unitTests.NioSocket.processSelectionKey(NioSocket.java:115)
     at tv.tvgate.unitTests.NioSocket.<init>(NioSocket.java:81)
     at tv.tvgate.unitTests.NioSocket.main(NioSocket.java:196)


What does it means? How can I solve this problem?

   Thnaks
0
Comment
Question by:dannysh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 7041873
 Java uses Unicode as its native character encoding. The MalformedInputException you are getting means that a byte (or character) sequence violates the rules of a multibyte character set or the rules of the surrogate extension mechanism for Unicode.

  Maybe this will you give you some ideas. http://java.sun.com/products/jdk/1.1/intl/html/intlspec.doc7.html

  Hope it helps.
0
 

Author Comment

by:dannysh
ID: 7041940
Hi girionis
I lloked at the article and they are talking about convertirng from byte to char and there is no java.io.CharToByteConverter class at all. Where can I find this class at all?
I am using java1.4 capablities, where can I find what does it means converting from ByteBuffer to string and what does the unicode symbol means at all?
   Thanks
0
 
LVL 35

Expert Comment

by:girionis
ID: 7041991
 You probably do not need the CharToByteConverter because they are obviously doing something else. What I wanted to show you is that they use some techniques to overcome problems such as MalformedInputExceptions, like checking the string length etc.
0
Industry Leaders: 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!

 

Author Comment

by:dannysh
ID: 7042010
ByteBuffer is new class included in java 1.4. As I understand, there are 4 parameters helping you navigate the buffer: mark,position,limit and capacity, and I don't know if it's possible to check if it's legal data.
Do you know how can I do it, or how can I overcome this problem? BTW I tried to use other encoding type:

Charset charset = Charset.forName("ISO-8859-1"); and it seems it works fine without any problem, do you know why?
   Thanks a lot
       
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7042642
simplest possible solution

String text = new String(byteBuffer.array())
0
 

Author Comment

by:dannysh
ID: 7043777
Hi heyhey
this solution has one problem that if the byteBuffer size is bigger then the number of bytes to be read, the text variable will also include an unnecessary information.

ByteBuffer buffer = new ByteBuffer[1024];
if this buffer read only 24 bytes and I will convert it to String, it will include also 1000 unused bytes.
   Thanks
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7043784
I do not hava Java 1.4 installed right now, so I cannot test any code.

did you try ?

String text = byteBuffer.asCharBuffer().toString();
0
 

Author Comment

by:dannysh
ID: 7043861
Hi heyhey
It still contains garbage. I realy don't understand why? If you will look at the code I wrote and the line you gave me, it looks the same and the only different is that I also add the encodeing type.
   
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7043870
> It still contains garbage

maybe it IS garbage after all :)

try printion all the bytes on the console to see what 'binary' data you are receiving.

0
 

Author Comment

by:dannysh
ID: 7049456
Hi
I still don't understand why it doesn't work well in other encoding.
   Danny
0

Featured Post

Industry Leaders: 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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

764 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