Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Efficient writable character buffers in Java

Reply
Thread Tools

Efficient writable character buffers in Java

 
 
yay_frogs@yahoo.com
Guest
Posts: n/a
 
      06-30-2006
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.

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      06-30-2006

<> wrote in message
news: oups.com...
> 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/



 
Reply With Quote
 
 
 
 
lordy
Guest
Posts: n/a
 
      06-30-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Buffers not Serializable in Java? bob Java 3 10-24-2011 04:02 AM
Java.NIO channel never becomes writable nooneinparticular314159@yahoo.com Java 6 03-17-2008 06:52 AM
Passing large C buffers to Java (via JNI) without copying? jpknott@gmail.com Java 10 10-28-2005 09:33 AM
Create a writable folder in aspnet setup project roger ASP .Net 2 10-23-2004 05:50 AM
Re: System.ArgumentException: Stream was not writable? Craig Deelsnyder ASP .Net 1 07-19-2004 07:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57