carfield.com.hk Ascii Art Formatter.java 2004-08-27T16:00:00Z 2004-08-27T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">import java.io.File; import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.Formatter; import java.util.logging.LogRecord; import java.util.logging.SimpleFormatter; import java.util.logging.Level; import java.util.Arrays; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.Raster; import java.awt.geom.Rectangle2D; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; import java.awt.font.TextLayout; /** * see http://weblogs.java.net/pub/wlg/1770 */ public class AsciiArtFormatter extends Formatter { private final Formatter formatter; private final Font font; private final Level level; public AsciiArtFormatter() { this(null); } public AsciiArtFormatter(Formatter formatter) { this(formatter, null, null); } /** * Creates a formatter that will additionally use ascii art to log the message in the * given font if its log level is at least level. * * @param formatter used to format the message before appending ascii art message. * If null a default SimpleFormatter is used. * @param level minimum level needed to enable ascii art logging. * If null Level.SEVERE is used. * @param font font used for any ascii art output. * If null a sans serif font of size 18 is used. */ public AsciiArtFormatter(Formatter formatter, Level level, Font font) { this.formatter = (formatter == null ? new SimpleFormatter() : formatter); this.level = level == null ? Level.SEVERE : level; this.font = font == null ? new Font(&quot;SansSerif&quot;, Font.PLAIN, 18) : font; } public String format(LogRecord record) { String message = formatter.format(record); if(record.getLevel().intValue() &gt;= level.intValue()) { message = message + getAsciiArt(font, record.getMessage()); } return message; } /** * @return the text as 'Ascii Art' in the given font using a '#' character for * each pixel (the text itself if any runtime exception occurs) */ public static String getAsciiArt(Font font, String text) { try { if(text == null) { text = &quot;(null)&quot;; } int tabWidth = 4; text = text.replaceAll(&quot;\\t&quot;, repeat(' ', tabWidth)); FontRenderContext fontRenderContext = new FontRenderContext(null, false, false); StringBuffer buffer = new StringBuffer(); String[] lines = text.split(&quot;\n|\r\n&quot;, 0); for(int lineIndex = 0; lineIndex &lt; lines.length; lineIndex++) { // How stupid: TextLayout does not handle white space like I want to, and // GlyphVector has no convenient method to calculate overall ascent/descent. // So just use both... String line = lines[lineIndex]; GlyphVector glyphVector = font.createGlyphVector(fontRenderContext, line); Rectangle2D bounds = glyphVector.getLogicalBounds(); int width = (int) bounds.getWidth(); int height = (int) bounds.getHeight(); if(line.length() == 0) { buffer.append(repeat('\n', height)); continue; } TextLayout textLayout = new TextLayout(line, font, fontRenderContext); float ascent = textLayout.getAscent(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); Graphics2D graphics = image.createGraphics(); try { graphics.drawGlyphVector(glyphVector, 0, ascent); Raster raster = image.getRaster(); int[] ints = new int[1]; for(int y = 0; y &lt; height; ++y) { for(int x = 0; x &lt; width; ++x) { raster.getPixel(x, y, ints); int pixelValue = ints[0]; buffer.append(pixelValue &gt; 0 ? '#' : ' '); } buffer.append('\n'); } } finally { graphics.dispose(); } } return new String(buffer); } catch(RuntimeException e) { // Don't want anything silly to happen only because using this fun method. // Revert to plain output ;-( return text; } } /** * @return a string consisting of the character c repeated count times. */ public static String repeat(char c, int count) { char[] fill = new char[count]; Arrays.fill(fill, c); return new String(fill); } /** * main class to show how to use it */ public static void main(String[] args) throws IOException { String userHome = System.getProperty(&quot;user.home&quot;); File logFile = new File(userHome, &quot;bigSevereText.log&quot;); String logFilePath = logFile.getPath(); System.out.println(&quot;Logging to file &quot; + logFilePath); Logger logger = Logger.getLogger(&quot;default&quot;); logger.setLevel(Level.FINEST); FileHandler logFileHandler = new FileHandler(logFilePath); logFileHandler.setFormatter(new AsciiArtFormatter()); logger.addHandler(logFileHandler); logger.fine(&quot;Fox prepares to jump.&quot;); logger.info(&quot;Fox is ready to jump.&quot;); logger.severe(&quot;Fox failed\nto jump over\nlazy dog.&quot;); logger.info(&quot;Dog is chasing fox.&quot;); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-08-27T16:00:00Z Convert Encoding.java 2004-08-10T16:00:00Z 2004-08-10T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">/* * 01/07/2003 - 15:19:32 * * ConvertEncoding.java - * Copyright (C) 2004 karneim.com Gesellschaft fuer Softwarearchitektur mbH * http://www.karneim.com * Author: Michael Karneim * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.io.*; import java.nio.charset.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ConvertEncoding { /** * Usage: java -jar Util.jar list * java -jar Util.jar test -n &lt;num_of_bytes&gt; [-o &lt;offset&gt;] [-tenc &lt;to_encoding&gt;] [-ffile &lt;from_file&gt;] [-tfile &lt;to_file&gt;|-gui &lt;yes|no&gt;] * java -jar Util.jar convert [-fenc &lt;from_encoding&gt;] [-tenc &lt;to_encoding&gt;] [-ffile &lt;from_file&gt;] [-tfile &lt;to_file&gt;] * java -jar Util.jar convertfiles [-fenc &lt;from_encoding&gt;] [-tenc &lt;to_encoding&gt;] [-tdir &lt;to_file&gt;] files.. * @param args String[] */ public static void main(String[] args) { if ( args.length == 0) { printUsage(); System.exit( 0); } String cmd = args[0]; Properties options = new Properties(); LinkedList fileList = new LinkedList(); for( int i=1; i&lt;args.length;) { String name = args[i]; if ( name.startsWith( &quot;-&quot;) == false) { fileList.add( name); i++; } else { String value = args[i + 1]; options.setProperty( name, value ); i=i+2; } } if ( &quot;list&quot;.equals( cmd)) { printEncodings(); } else if ( &quot;convert&quot;.equals( cmd) ) { InputStream in = null; { String fromFile = options.getProperty( &quot;-ffile&quot;); if ( fromFile != null) { try { in = new FileInputStream(fromFile); } catch (FileNotFoundException ex) { System.err.println(&quot;File '&quot;+fromFile+&quot;' not found.&quot;); System.exit( -1); } } else { in = System.in; } } OutputStream out = null; { String toFile = options.getProperty( &quot;-tfile&quot;); if ( toFile != null) { try { File f = new File( toFile); out = new FileOutputStream( f); } catch (FileNotFoundException ex) { throw new Error( &quot;huh? &quot;+ex); } } else { out = System.out; } } String fromEncoding = options.getProperty( &quot;-fenc&quot;); String toEncoding = options.getProperty( &quot;-tenc&quot;); try { convertStream( in, fromEncoding, toEncoding, out ); } catch ( UnsupportedEncodingException ex1 ) { System.err.println(&quot;Unsupported encoding '&quot;+ex1.getMessage()+&quot;'.&quot;); System.exit( -1); } catch ( IOException ex2) { System.err.println( &quot;Error while reading/writing: ex=&quot; + ex2 ); System.exit( -1); } } else if ( &quot;convertfiles&quot;.equals( cmd) ) { String toDir = options.getProperty( &quot;-tdir&quot;, &quot;.&quot;); String fromEncoding = options.getProperty( &quot;-fenc&quot;); String toEncoding = options.getProperty( &quot;-tenc&quot;); String[] files = (String[])fileList.toArray( new String[0]); try { convertFiles( files, fromEncoding, toEncoding, toDir ); } catch ( UnsupportedEncodingException ex1 ) { System.err.println(&quot;Unsupported encoding '&quot;+ex1.getMessage()+&quot;'.&quot;); System.exit( -1); } catch ( IOException ex2) { System.err.println( &quot;Error while reading/writing: ex=&quot; + ex2 ); System.exit( -1); } } else if ( &quot;test&quot;.equals( cmd)) { InputStream in = null; { String fromFile = options.getProperty( &quot;-ffile&quot;); if ( fromFile != null) { try { in = new FileInputStream(fromFile); } catch (FileNotFoundException ex) { System.err.println(&quot;File '&quot;+fromFile+&quot;' not found.&quot;); System.exit( -1); } } else { in = System.in; } } String toEncoding = options.getProperty( &quot;-tenc&quot;); int numOfBytes = Integer.parseInt( options.getProperty( &quot;-n&quot;, &quot;-1&quot;)); int offset = Integer.parseInt( options.getProperty( &quot;-o&quot;, &quot;0&quot;)); boolean gui = options.getProperty( &quot;-gui&quot;,&quot;no&quot;).equals( &quot;yes&quot;); OutputStream out = null; if ( gui ) { out = new ByteArrayOutputStream( numOfBytes); } else { String toFile = options.getProperty( &quot;-tfile&quot;); if ( toFile != null) { try { File f = new File( toFile); out = new FileOutputStream( f); } catch (FileNotFoundException ex) { throw new Error( &quot;huh? &quot;+ex); } } else { out = System.out; } } try { test( in, toEncoding, out, numOfBytes, offset); if ( out instanceof ByteArrayOutputStream) { String content = null; if ( toEncoding != null) { content = new String( ( ( ByteArrayOutputStream ) out ).toByteArray(), toEncoding ); } else { content = new String( ( ( ByteArrayOutputStream ) out ).toByteArray() ); } JFrame f = new JFrame(&quot;Output&quot;); JTextArea ta = new JTextArea(); JScrollPane p = new JScrollPane( ta); f.getContentPane().add( p, BorderLayout.CENTER); f.setSize( 600, 500); f.setLocation( 100,100); ta.setFont( new Font(&quot;Monospaced&quot;, Font.PLAIN, 12)); ta.setText( content); f.addWindowListener( new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit( 0); } } ); f.show(); } } catch ( UnsupportedEncodingException ex) { throw new Error( ex); } catch ( IOException ex) { System.err.println( &quot;Error while reading/writing: ex=&quot; + ex ); ex.printStackTrace(); System.exit( -1); } } else { printUsage(); } } public static void printUsage() { System.out.println(&quot;Usage: java -jar Util.jar &quot;+ConvertEncoding.class.getName()+&quot; list&quot;); System.out.println(&quot; - lists all available encodings&quot;); System.out.println(&quot; java -jar Util.jar &quot;+ConvertEncoding.class.getName()+&quot; test -n &lt;num_of_bytes&gt; [-o &lt;offset&gt;] [-tenc &lt;to_encoding&gt;] [-ffile &lt;from_file&gt;] [-tfile &lt;to_file&gt;|-gui &lt;yes|no&gt;] &quot;); System.out.println(&quot; - converts the num_of_bytes bytes starting from offset of the from_file into to_file, using every known encoding for read and to_encoding for write, into separate lines&quot;); System.out.println(&quot; java -jar Util.jar &quot;+ConvertEncoding.class.getName()+&quot; convert [-fenc &lt;from_encoding&gt;] [-tenc &lt;to_encoding&gt;] [-ffile &lt;from_file&gt;] [-tfile &lt;to_file&gt;]&quot;); System.out.println(&quot; - converts the content of the from_file into to_file, using the encoding from_encoding for read and to_encoding for write&quot;); System.out.println(&quot; java -jar Util.jar &quot;+ConvertEncoding.class.getName()+&quot; convertfiles [-fenc &lt;from_encoding&gt;] [-tenc &lt;to_encoding&gt;] [-tdir &lt;to_file&gt;] files..&quot;); System.out.println(&quot; - converts the specified files, using the encoding from_encoding for read and to_encoding for write&quot;); } public static void convertFiles( String[] files, String fromEncoding, String toEncoding, String toDir) throws UnsupportedEncodingException, IOException { File toDirFile = new File( toDir); if ( toDirFile.exists() == false) { throw new IOException(&quot;toDir '&quot;+toDir+&quot;' does not exist&quot;); } File currDir = new File(&quot;.&quot;); if ( currDir.equals( toDirFile) ) { throw new IOException(&quot;Can not overwrite files in directory '&quot;+toDir+&quot;', since I am reading from there. Specify a different to_dir directory.&quot;); } for( int i=0; i&lt;files.length; ++i) { InputStream in = null; File inFile = null; { String fromFile = files[i]; try { inFile = new File( fromFile); in = new FileInputStream( inFile ); } catch ( FileNotFoundException ex ) { System.err.println( &quot;File '&quot; + fromFile + &quot;' not found.&quot; ); System.exit( -1 ); } } OutputStream out = null; { String toFile = inFile.getName(); if ( toFile != null) { try { File f = new File( toDirFile, toFile); out = new FileOutputStream( f); } catch (FileNotFoundException ex) { throw new Error( &quot;huh? &quot;+ex); } } else { out = System.out; } } System.out.println(&quot;[INFO] Converting &quot;+files[i]+&quot;.&quot;); convertStream( in, fromEncoding, toEncoding, out); } System.out.println(&quot;Finished.&quot;); } public static void convertStream( InputStream in, String fromEncoding, String toEncoding, OutputStream out) throws UnsupportedEncodingException, IOException { InputStreamReader r = null; try { if ( fromEncoding != null) { r = new InputStreamReader( in, fromEncoding ); } else { r = new InputStreamReader( in ); System.err.println(&quot;from_encoding undefined. Using default encoding '&quot;+r.getEncoding()+&quot;'&quot;); } } catch ( UnsupportedEncodingException ex ) { throw new UnsupportedEncodingException( fromEncoding); } OutputStreamWriter w = null; try { if ( toEncoding != null) { w = new OutputStreamWriter( out, toEncoding ); } else { w = new OutputStreamWriter( out); System.err.println(&quot;to_encoding undefined. Using default encoding '&quot;+w.getEncoding()+&quot;'&quot;); } } catch ( UnsupportedEncodingException ex1 ) { throw new UnsupportedEncodingException( toEncoding); } char[] buf = new char[1024*4]; int read = r.read( buf); while( read &gt; 0) { w.write( buf, 0, read); read = r.read( buf); } w.close(); r.close(); } public static void test( InputStream in, String toEncoding, OutputStream out, int numOfBytes, int offset) throws UnsupportedEncodingException, IOException { in.skip( offset); byte[] content = new byte[ numOfBytes]; in.read( content); in.close(); InputStreamReader r = null; OutputStreamWriter w = new OutputStreamWriter( out); String[] encodings = getEncodings(); int maxLenght = 0; for( int i = 0; i&lt; encodings.length; ++i) { if ( encodings[i].length() &gt; maxLenght ) { maxLenght = encodings[i].length(); } } for( int i = 0; i&lt; encodings.length; ++i) { String fromEncoding = encodings[i]; // System.err.println(&quot;[DEBUG] fromEncoding=&quot;+fromEncoding); try { ByteArrayInputStream bin = new ByteArrayInputStream( content); r = new InputStreamReader( bin, fromEncoding ); } catch ( UnsupportedEncodingException ex ) { throw new UnsupportedEncodingException( fromEncoding ); } w.write( &quot;\n&quot;.toCharArray()); w.write( fromEncoding.toCharArray()); for( int s=0; s&lt;maxLenght - fromEncoding.length(); ++s) { w.write( &quot; &quot;.toCharArray()); } w.write( &quot;|&quot;.toCharArray()); char[] buf = new char[ 1024*4]; int read = r.read( buf ); while ( read &gt; 0 ) { w.write( buf, 0, read ); read = r.read( buf ); } r.close(); } w.write( &quot;\n&quot;.toCharArray()); w.close(); } public static String[] getEncodings() { Map availcs = Charset.availableCharsets(); String[] result = new String[ availcs.size()]; Set keys = availcs.keySet(); Iterator iter = keys.iterator(); for (int i=0; iter.hasNext(); ++i) { result[i] = (String)iter.next(); } return result; } public static void printEncodings() { Map availcs = Charset.availableCharsets(); Set keys = availcs.keySet(); for (Iterator iter = keys.iterator(); iter.hasNext(); ) { System.out.println(iter.next()); } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-08-10T16:00:00Z Java Memory Monitor.html 2004-03-31T16:00:00Z 2004-03-31T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-31T16:00:00Z Send File To Browser.html 2002-11-22T16:00:00Z 2002-11-22T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-11-22T16:00:00Z Test security.java.html 2002-07-05T16:00:00Z 2002-07-05T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-07-05T16:00:00Z http tunnel proxy.java.html 2001-09-28T16:00:00Z 2001-09-28T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-09-28T16:00:00Z Image2vrml.java.html 2001-09-21T16:00:00Z 2001-09-21T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-09-21T16:00:00Z htaccess auth.java.html 2001-09-07T16:00:00Z 2001-09-07T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-09-07T16:00:00Z Sort Object.java.html 2001-07-26T16:00:00Z 2001-07-26T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-07-26T16:00:00Z Mouse Hex Value Check.html 2001-07-03T16:00:00Z 2001-07-03T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-07-03T16:00:00Z compression.java.html 2001-07-03T16:00:00Z 2001-07-03T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-07-03T16:00:00Z