Does it make sense to make encoder and descoder as static private variables, instead of initiating them every time?
Should I invoke fileChannel_.force(false) while reading every time a record is read, or it is Ok now?
I synchronize on database, not record. Does it make sense allocate Byte/CharBuffer only one time, or ir is Ok now leaving a possibility to switch synchronization to record-level without changing a IO read/write methods?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
you use cache_ fileHeader_ these kinds of instance variable
so this method is somehow doing some in the database related to money.
Why is the instantiation of the record outside the if block?
Sorry, I don't understand what you are asking for here. I don't see the connection betwen "synchronizing on the database or record"
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.
Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
Yes, I would allocate that ByteBuffer only once outside of the loop. Think of the fact that that ByteBuffer is a local variable, in such a way that whatever your design, it will never get accessed by some other thread.
You don't need to allocate your CharBuffer chars, because as a few lines later you assign "charset.newDecoder().decode(bytes)" to it
One more little possible optimization IMO : you don't need the newDecoder() part in the expression
1) you decode for each field separately, allocating implicetely CharArray for each field!?
2) I could decode the whole record in string, but if the record is very large in future (let's say 5 KB) it would be probably better first deciode it in CharBuffer and then fill step by step field String from the Buffer.
Originally posted by Vlad Rabkin:
Hi,
It is very common to use xxx_ for private member variables. I don't need to use this.xxx to differ them for local ones. I was wondering also as I have read some books and saw code sample with private variables having xxx_ signature (e.g. "RMI" from O'Relly). I had a project a year ago, where I met a freelancer with experience over 25 years. He was the best deveoper and architect I have ever seen. He used the same thing. So, I decided to use it also.
Do you think Sun will not approciate it?
Vlad
However, it's not(again, IMO) very java-like.
1) Honestly saying I don't really understand why it should be much faster since CharSet.decode() must now allocate internally an CharBuffer() for every field, not a record!?
2) I didn't change write method for CharSet.newEncoder().encode()-> CharSet.encode(), because CharBuffer is one byte less, than ByteBuffer, and it is impossible then to add this byte (status flag) without creating a new ByteBuffer. Of course, I could allocate CharBuffer with the same number of byte, but what for: CharBuffer doesn't have status flag (active/deleted)?
I am pretty sure the second solution is more efficient. Now if you really want to know, you may test
all your fields may write themselves in the ByteBuffer without any need for an intermediate CharBuffer
As you can see, I use much "delegation" in my design because I find it easy to do so, but sometimes I wonder if it is good practice and easy to be understood by others :
I have 1 suggestion and one question to you:
1. Arrays.fill(b, (byte) 0); is not needed, because when an ByteBuffer is allocated it has all his bytes set to 0. Probably you allocate it only one time, not by each write, but in this case it would easer just to clear() method on ByteBuffer().
public final Buffer clear()
Clears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:
buf.clear(); // Prepare buffer for reading
in.read(buf); // Read data
This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.
2. metaData.charset.encode(value); I don't know if encode() method is synchrone. Specification says Encoder will be cached for optimization.
If you we want to allow in future concurrent writes on different records:
ByteBuffer is Ok, since it will be a local variable, but Encoder will be the same object used by all threads, since charset is static. It is not then thread safe in case of record based synchronization. Isnt't it?
Buffer clear()
This method does not actually erase the data in the buffer
We don't allow concurrent writes, but I do allow concurrent reads !
Good team work, isn't it ?
"I'm not back." - Bill Harding, Twister
An invocation of this method upon a charset cs returns the same result as the expression
cs.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.decode(bb);
except that it is potentially more efficient because it can cache decoders between successive invocations.
...encode/decode the whole ByteBuffer at once.
but they don't "override the default options" of decode().
If don't, why shouldn't we then use encode()/decode() convinience methods as Jim mentioned?
All of the methods defined in this class are safe for use by multiple concurrent threads.
It seems that you are explaining to me things that I already understood and ... explained myself before
Yes they are ugly.... . I will not keep them anyway.
I'll use Charset.decode() / encode() as I did before
"I'm not back." - Bill Harding, Twister