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
Solved

ByteBuffer read problem

Posted on 2002-05-29
10
464 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
jboss 7.1 start up error 1 31
SequenceInputStream example 3 19
Fast way to search item into Java Array (Rhino compatible) 2 31
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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

839 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