|
JavaFAQ Home » Java Newsletters

****************************************************************** *
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * * > The Java FAQ
Daily Tips, weekly publication < * *
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * * * * Issue No: 8 24
Oktober 2000 * * http://www.javafaq.nu/java * * * * * * Please
recommend us to your friends and colleagues!
* ******************************************************************
Table
of Contents
> How to get the size of an Enumeration object.. >
How does BufferedReader work and how to use it to get input from a
keyboard? > Do exist Compound Files in Java? > About Vector
initializing... > RMI versus Socket communication-1 > RMI versus
Socket communication-2 > Bitwise operations change the 0/1 bits of a
number.
Why? ******************************************************************
Tip
1
Does anyone know how could I get the size of an Enumeration object?
The API for Enumeration only contains getNext() and next().
A1: You
can't. Theoretically, some classes that implement Enumeration may also
provide some way to get a size, but you'd have to know about the more
specific run-time type and cast to it... and none of the standard java.util
Collections classes nor Vector or such provide these methods in their
Enumeration implementations.
A2: you can make your own class like
this:
import java.util.*;
public class MyEnumeration{ int
size; int index = 0; Enumeration e;
public MyEnumeration(Vector
v){ size = v.size(); e = v.elements(); index = 0; }
public
boolean hasMoreElements(){ return e.hasMoreElements(); }
public
Object nextElement(){ index++; return e.nextElement(); }
public
int size(){ return size; }
public int getIndex(){ return
index; } }
by Nicolas Delbing lambs@mail1.stofanet.dk and
Victor Vishnyakov mailto:tcch_@mail.ru
******************************************************************
Tip
2
Can anyone please explain clearly how BufferedReader works and how
to use it to get input from a keyboard? Q: Can anyone please explain clearly
how BufferedReader works and how to use it to get input from a
keyboard?
A: BufferedReader is a filter reader class. That is, it
wraps another reader and reading from it is like reading from the reader it
wraps, except that it changes something. In the case of BufferedReader, it
reads in large chunks and then you can retrieve its data in smaller bits. To
use it to read from System.in, you first need a reader to wrap. You can
bridge from an input stream (which System.in is) to a reader by using an
InputStreamReader. Then wrap that in a BufferedReader as
follows:
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
Now you can call methods of BufferedReader
to read from standard input. Generally, you create a BufferedReader to be
able to call the readLine() method. That isn't BufferedReader's main
intended use -- the main intended use is performance -- but you don't
generally care too awfully much about performance of reads from the
console. So call readLine to get a line of input, which will be null on end
of stream (user presses Ctrl-D on UNIX or a file was redirected in and is
done).
******************************************************************
Tip
3
Do exist Compound Files in Java?
Microsoft has made an API
where you can have a whole "virtual" filesystem inside a single file on the
real filesystem. I think they call it Compound Files - I have also heard
about the concept under the name of Structured Storage.
Do any of you
know if something like that exists in a Java library? I have to work for
all platforms! (It can of cause be implemented with the use of Compound
Files in its Windows implementation) Answer: jar files are essentially the
same.
Have a look at java.util.jar.JarFile
Tip
4
Is there a way to provide values for a Vector in the source code,
analogous to array initializers?
Answer: The Vector class constuctors
take no arguments other than Collection (since JDK 1.2), which is abstract,
and since a Vector is a structure whose size can change dynamically, its
contents can only be initialaized through member methods.
******************************************************************
Tip
5
RMI versus Socket communication
Q: I wish to get Java talking to
C++ across a network. Does anyone have any thoughts in terms of performance,
ease of development etc. in : Wrapping the C++ side with JNI and
using RMI for the communications. versus Writing sockets code and
communicating via http?
A: It depends of what kind of application you're
writing but l think about the following:
- with RMI you can have
remote REFERENCE instead of having to transfer all the object through the
network. The object has just to implement Remote. So it spare bandwith and is
good for performance. This is impossible to do if you do through a
socket connection, you've to send the all object. - You've not to take in
charge the serialization (which could be not so easy depending of your
object structure), neither the connections, etc... All of that is taken in
charge by RMI. - the performance are GOOD (even a bit more than that)
three good points to use RMI, isn't it?
The difficulty added by RMI
is the configuration of both client and server (distribution of stubs,
rmiregistry, what's happen if firewall). Depending of the environment all of
that can be either easy or complicate. But once that all of that is in
place you can extend your application easily, so it's much more flexible and
scalable.
If your needs are small perhaps that you could do your own
connection system (but for me it's less scalable and more bandwith
consuming and so less performant).
******************************************************************
Tip
6
RMI versus Socket communication
Q: I wish to get Java talking to
C++ across a network. Does anyone have any thoughts in terms of performance,
ease of development etc. in : Wrapping the C++ side with JNI and
using RMI for the communications. versus Writing sockets code and
communicating via http?
see please first part of answer in yesterday's
tip.
Answer2: I have done both. If your communication scenarios are
diverse and could keep changing, using a remote technology like RMI can
help. If the operations are few and/or not likely to change you can save the
JNI complexity. Not that it is really hard it just can be fun keeping the
JNI code in sinc with the C++ code.
******************************************************************
Tip
7
I understand that bitwise operations change the 0/1 bits of a
number. Question is why? I suppose it's interesting that you can
manipulate numbers this way, but I can't think of a practical use for doing
that. Can anyone help me understand when are bitwise operations used
and why you would use them?
Answer: Bitwise manipulation is often used
where memory consumption is critical, and a piece of information may be
encoded in less that one byte, for instance. In communication software and
protocols, information may be interpreted as a stream of bits where the
information is encoded at the bit-level, and you use bitwise manipulation to
extract the pieces of information encoded in the bytes. There are other
situations where bitwise manipulation is used, as well.
The
Java FAQ Daily Tips is a newsletter that is only sent to those who have
specifically subscribed to it.
John Andersson, Editor
mailto:info@javafaq.nu
To subscribe to The Java FAQ Daily print in
subject field: "Subscribe" and mailto:subscription@javafaq.nu
To
unsubscribe print in subject field:
"Unsubscribe" mailto:subscription@javafaq.nu
Copyright (c) 2000 John
Andersson ------------------------------------------------------------------- You
can find our tips on site also! Please recommend us to your friends and
colleagues!
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|