• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

ByteBuffer read problem

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
dannysh
Asked:
dannysh
  • 5
  • 3
  • 2
1 Solution
 
girionisCommented:
 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
 
dannyshAuthor Commented:
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
 
girionisCommented:
 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dannyshAuthor Commented:
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
 
heyhey_Commented:
simplest possible solution

String text = new String(byteBuffer.array())
0
 
dannyshAuthor Commented:
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
 
heyhey_Commented:
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
 
dannyshAuthor Commented:
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
 
heyhey_Commented:
> 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
 
dannyshAuthor Commented:
Hi
I still don't understand why it doesn't work well in other encoding.
   Danny
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now