27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

General <strong>Java</strong> Questions I<br />

--<br />

Eric<br />

Q: can anyone provide an example on how to use clone() and clonable interface?<br />

Answer:<br />

//Class Point uses default clone implementation that returns<br />

//shallow copy of Point (which is sufficient in this case).<br />

class Point implements Cloneable {<br />

int x, y;<br />

}<br />

class ImmutableRectangle {<br />

private Point lowerLeft = new Point();<br />

private Point upperRight = new Point();<br />

public Point getLowerLeftPoint() {<br />

try {<br />

return lowerLeft.clone();<br />

}<br />

catch(CloneNotSupportedException e) {<br />

//shouldn't occur since Point is cloneable<br />

throw new InternalError();<br />

}<br />

}<br />

}<br />

we don't want Rectangle points to be changed outside its class. the best<br />

way would be to create a copy of Point object and pass it.<br />

i5mast<br />

MVP for <strong>Java</strong> 2<br />

http://www.brainbench.com<br />

Q: I am looking for more efficient compression APIs esp. for *.doc and *.html files.<br />

I need some <strong>Java</strong> APIs wherein I can implement various compression algorithms. I<br />

have already the GZIP compression technique shipped with JDK's java.util.zip<br />

package. I am looking for more efficient compression APIs esp. for *.doc and *.html<br />

files.<br />

Answer: You often get better compression results for a large number of smaller files<br />

by concatenating the uncompressed files and then compressing the result. That's<br />

why tar.gz is often superior to zip for large numbers of html files (like API docs).<br />

You will not get much better general-purpose compression than Zip, at least not until<br />

you have a specific class of data and know very much about its structure. Even then<br />

you'll need some experience and time working on a better compression method.<br />

You might want to take a look at this <strong>Java</strong> implementation of bzip2:<br />

http://www.aftexsw.com/aftex/products/java/bzip/<br />

It uses more CPU cycles and memory but typically compresses better<br />

than zip.<br />

--<br />

Marco<br />

Q: I need to programmatically replace an entry in a zip file.<br />

file:///F|/350_t/350_tips/general_java-I.htm (22 of 31) [2002-02-27 21:18:17]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!