Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
T E C H
T I P S
TIPS, TECHNIQUES, AND SAMPLE CODE
java.awt.Image;
java.awt.Graphics2D;
java.awt.geom.AffineTransform;
java.awt.image.BufferedImage;
java.io.IOException;
java.io.OutputStream;
java.io.FileOutputStream;
javax.swing.ImageIcon;
com.sun.image.codec.jpeg.JPEGCodec;
com.sun.image.codec.jpeg.JPEGImageEncoder;
class Thumbnail {
public static void main(String[] args) {
createThumbnail(args[0], args[1], Integer.parseInt(args[2]));
}
/**
* Reads an image in a file and creates a thumbnail in another file.
* @param orig The name of image file.
* @param thumb The name of thumbnail file. Will be created if necessary.
try {
logfile.write(buf, off, len);
} catch (Exception e) {
e.printStackTrace();
setError();
}
super.write(buf, off, len);
}
}
The start() method first saves the current standard output and
standard error print streams. These print streams will be restored
when stop() is called. Next, the log file is opened. If the log
file does not exist, it's created. Otherwise, the log file is
emptied. Finally, System.setOut() and System.setErr() are called
to replace the standard output and standard error print streams with
SaveOutput print streams.
The stop () method restores the original standard output and
standard error. It then closes the log file.
A SaveOutput object is a PrintStream object that acts like a tee.
Any characters it receives are forwarded to two places: the log file
and the underlying print streams. The underlying print streams in
the example are the original standard output and standard error
print streams; these are supplied to the SaveOutput constructor.
Although both standard output and standard error are written into the
same logfile, there is no need to synchronize this operation. The
reason is that the logfile output stream is itself a print stream
and write operations are synchronized.
To implement this tee behavior, the program needs to override the
two forms of the write method. These overrides simply write the
characters into the logfile and then to the underlying print stream
(by calling super.write()). The write() methods do not throw
exceptions. Instead, they set a flag in the print stream if some
problem occurs. They set the flag by calling setError(). If the client
of the print stream wants to check if an error occurred, it can call
checkError().
. . . . . . . . . . . . . . . . . . . . . . . .
- NOTE
The names on the JDC mailing list are used for internal Sun
Microsystems(tm) purposes only. To remove your name from the list,
see Subscribe/Unsubscribe below.
- FEEDBACK
Comments? Send your feedback on the JDC Tech Tips to:
jdc-webmaster@java.sun.com
- SUBSCRIBE/UNSUBSCRIBE
The JDC Tech Tips are sent to you because you elected to subscribe
when you registered as a JDC member. To unsubscribe from JDC Email,
go to the following address and enter the email address you wish to
remove from the mailing list:
http://developer.java.sun.com/unsubscribe.html
To become a JDC member and subscribe to this newsletter go to:
http://java.sun.com/jdc/
- ARCHIVES
You'll find the JDC Tech Tips archives at:
http://developer.java.sun.com/developer/TechTips/index.html
- COPYRIGHT
Copyright 1999 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.
This document is protected by copyright. For more information, see:
http://developer.java.sun.com/developer/copyright.html