Re: Efficient writable character buffers in Java




<yay_frogs@xxxxxxxxx> wrote in message
news:1151685824.501525.319580@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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 matth@xxxxxxxxxxxxxx http://www.iviz.com/



.



Relevant Pages

  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... Why is using char* a bad thing and why using sprintf a bad thing to, ... can be up to MAX_PATH characters). ... LPSTR lpMsgBuf; ... MessageBox(NULL, lpMsgBuf, "GetLastError() for ...
    (microsoft.public.vc.mfc)
  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... This means that if you develop the bad habit of using char * (left over ... It usually takes me five minutes to create a Unicode version of any of my apps, ... BOOL and bool are different data types. ... can be up to MAX_PATH characters). ...
    (microsoft.public.vc.mfc)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.server)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.programming)
  • Re: RfD: XCHAR wordset (for UTF-8 and alike)
    ... extended to work with xchars, ... >replacing one char with another. ... before-cursor part, even for fixed-width characters. ... So, should string words ...
    (comp.lang.forth)