Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

trouble with placing strings into charbuffers

Thread view: 
nooneinparticular314159@yahoo.com - 18 Apr 2006 13:14 GMT
I'm trying to create a bytebuffer of a specific size, create a
charbuffer view of that bytebuffer, and write a string into that
charbuffer.  My bytebuffer always needs to be 4 bytes long (represent a
4 digit number), but no matter how I try to place some chars into the
buffer, I always get an overflow.  For example:

       //Test fixed length header strings
       String ZeroString = "0000";
       byte[] ByteSizeArray = ZeroString.getBytes();
       int MessageHeaderSize = ByteSizeArray.length;
//        int MessageHeaderSize = 4;
       System.out.println("Length in bytes of 0000 = "+
MessageHeaderSize);
       ByteBuffer HeaderBuffer =
ByteBuffer.allocateDirect(MessageHeaderSize);
       System.out.println("HeaderBuffer.capacity() = "+
HeaderBuffer.capacity());
       HeaderBuffer.put(ByteSizeArray);

       StringBuffer HeaderStringBuffer = new StringBuffer(ZeroString);
       CharBuffer HeaderCharBuffer = HeaderBuffer.asCharBuffer();
//        Character ZeroChar = new Character("0");

       for (int counter = 0; counter < HeaderBuffer.capacity();
counter++){
           HeaderCharBuffer.put(counter, (char) 0);
       }

Can someone tell me what I'm doing wrong here?

Thanks!
grasp06110@yahoo.com - 18 Apr 2006 13:58 GMT
Does the solution shown below work for you?  It looks like the call to
headerBuffer.asCharBuffer(); was returning a zero capacity buffer.  The
loop was using the capacity of the other buffer as a counter.
Therefore, the loop was entered (capacity of the other buffer was 4)
and an attempt was made to add data to the zero capacity buffer.

What IDE are you using?  Eclipse has some nice debugging tools that
might have been helpful.

Hope this helps,
John

/**
*
*/
package com.mydomain.example;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

/**
* @author greshje
*
*/
public class Example {

    public static void main(String[] args) throws Exception {
        // Test fixed length header strings
        String zeroString = "0000";
        byte[] bytes = zeroString.getBytes();
        int messageHeaderSize = bytes.length;
        // int MessageHeaderSize = 4;
        System.out.println("Length in bytes of 0000 = "
                 + messageHeaderSize);
        ByteBuffer headerBuffer =
                 ByteBuffer.allocateDirect(messageHeaderSize);
        System.out.println("HeaderBuffer.capacity() = "
                + headerBuffer.capacity());
        headerBuffer.put(bytes);

        StringBuffer headerStringBuffer = new StringBuffer(zeroString);
        // CharBuffer headerCharBuffer = headerBuffer.asCharBuffer();
        CharBuffer headerCharBuffer = CharBuffer.allocate(4);
        for (int counter = 0;
                  counter < headerCharBuffer.capacity();
                  counter++) {
            System.out.println(counter);
            headerCharBuffer.put(counter, (char) 0);
        }
    }

}
nooneinparticular314159@yahoo.com - 18 Apr 2006 14:39 GMT
Wow.  Thanks!  That makes sense.  So basically, I need to be sure to
initialize the back end buffer, and what I was doing before didn't do
that.  :)

I use Netbeans.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.