Solved

ByteBuffer read problem

Posted on 2002-05-29
10
468 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 50 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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

749 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