trouble with placing strings into charbuffers

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, Apr 18, 2006.

  1. Guest

    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!
    , Apr 18, 2006
    #1
    1. Advertising

  2. Guest

    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);
    }
    }

    }
    , Apr 18, 2006
    #2
    1. Advertising

  3. Guest

    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.
    , Apr 18, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Chadwick

    Placing dataset into XPathDocument

    Keith Chadwick, Nov 23, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,243
    Stephen Cook
    Nov 24, 2003
  2. Jonathan Halterman

    Placing bytes into a stream

    Jonathan Halterman, Oct 16, 2003, in forum: C++
    Replies:
    5
    Views:
    380
    Jonathan Halterman
    Oct 16, 2003
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
  4. Replies:
    6
    Views:
    557
    CBFalconer
    Feb 17, 2007
  5. Michael W. Ryder

    Placing tabs in strings in irb

    Michael W. Ryder, Jan 23, 2007, in forum: Ruby
    Replies:
    14
    Views:
    179
    Michael W. Ryder
    Jan 23, 2007
Loading...

Share This Page