Efficient writable character buffers in Java

Discussion in 'Java' started by yay_frogs@yahoo.com, Jun 30, 2006.

  1. Guest

    My problem: I need to create a buffer that needs a capacity sufficient
    to hold a row of 80 characters that will be written out to a file, then
    modified, then written out to a file again. In C, this could be done
    like:

    char linebuf[81];

    /* code to insert chars in linebuf; as an example: */
    linebuf[0] = 'H';
    linebuf[1] = 'i';
    linebuf[2] = 0; /* null char to terminate string */

    /* write the line */
    printf("%s\n", linebuf); // "Hi" is written out followed by a
    newline.

    /* modify line buffer again; no need to allocate/delete memory */
    linebuf[0] = 'Y';
    linebuf[1] = 'o';
    linebuf[2] = '!';
    linebuf[3] = 0;

    /* write the line */
    printf("%s\n", linebuf); // "Yo!" is written out followed by a
    newline.


    Now my problem is that in Java, I can't figure out how to do this
    without allocating new objects. If a use a StringBuffer/StringBuilder
    that has a length of 80 characters, there doesn't seem to be a way to
    efficiently print only the number of characters that the current row
    actually has. (The setLength method creates a new object.) If I use a
    char[] in Java then the output routines ignore the terminating null
    byte.

    There has got to be a way to do this in Java! Thanks for any help.
    , Jun 30, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > My problem: I need to create a buffer that needs a capacity sufficient
    > to hold a row of 80 characters that will be written out to a file, then
    > modified, then written out to a file again. In C, this could be done
    > like:
    >
    > char linebuf[81];
    >
    > /* code to insert chars in linebuf; as an example: */
    > linebuf[0] = 'H';
    > linebuf[1] = 'i';
    > linebuf[2] = 0; /* null char to terminate string */
    >
    > /* write the line */
    > printf("%s\n", linebuf); // "Hi" is written out followed by a
    > newline.
    >
    > /* modify line buffer again; no need to allocate/delete memory */
    > linebuf[0] = 'Y';
    > linebuf[1] = 'o';
    > linebuf[2] = '!';
    > linebuf[3] = 0;
    >
    > /* write the line */
    > printf("%s\n", linebuf); // "Yo!" is written out followed by a
    > newline.
    >
    >
    > Now my problem is that in Java, I can't figure out how to do this
    > without allocating new objects. If a use a StringBuffer/StringBuilder
    > that has a length of 80 characters, there doesn't seem to be a way to
    > efficiently print only the number of characters that the current row
    > actually has. (The setLength method creates a new object.) If I use a
    > char[] in Java then the output routines ignore the terminating null
    > byte.


    I'll skip the standard warning against premature optimization because you
    seem determined that allocating memory is implicitly inefficient. A key
    problem with null-terminated strings, of course, is that you have to keep
    iterating to find the end.

    What's wrong with

    char [] buffer = new char [81];
    // Fill out the buffer

    writer.write (buffer, 0, lastIndexWritten + 1);

    OR

    writer.write (buffer, 0, positionOfZero(buffer) + 1);

    You either already know the length in advance and simply use it when writing
    data or you have a little iterator utility function that figures out where
    the zero is. Or just wrap the ordinary write (char) in a loop that stops
    after the zero.

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Jun 30, 2006
    #2
    1. Advertising

  3. lordy Guest

    On 2006-06-30, <> wrote:
    If I use a
    > char[] in Java then the output routines ignore the terminating null
    > byte.


    Are you sure about that? Are you passing the byte to the output
    routines??

    Lordy
    lordy, Jun 30, 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. Craig Deelsnyder
    Replies:
    1
    Views:
    4,624
    =?Utf-8?B?U2hhd24gS2VubmV5?=
    Jul 19, 2004
  2. roger
    Replies:
    2
    Views:
    1,008
    roger
    Oct 23, 2004
  3. hazz
    Replies:
    0
    Views:
    645
  4. Jason Curl

    Writable strings

    Jason Curl, Jun 22, 2005, in forum: C Programming
    Replies:
    10
    Views:
    1,457
    Default User
    Jun 23, 2005
  5. Replies:
    6
    Views:
    660
    Gordon Beaton
    Mar 17, 2008
Loading...

Share This Page