Solved

ByteBuffer read problem

Posted on 2002-05-29
10
460 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
  • 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now