2009年11月09日
【Commons】Compressでgzip,bzip2の解凍
Windowsで動かしてみたところ、実行速度はCygwinでコマンド使うよりも数倍遅い感じ。
具体的には5MBのbzip2を解凍するのに12秒、gzipだとなぜか22秒もかかる。ちなみにBufferedInputStream使っても、byte[8192]とか用意しても、時間は変わらない。
同サイズのファイル出力にかかる時間は1秒程度なので、解凍処理が遅いように見えるけど、どうなんだろう。こちらのコードがおかしかったりするだろうか?
続きを読む
2009年11月08日
【Commons】CompressでZIPの解凍
昔Sandboxにあるのを見かけた記憶があるのだけど、今はちゃんとリリース版になっているようだ。他にもzipやgzip、tar、cpioの展開なんかも出来てしまうらしい。
ArchiveStreamFactoryというのを使うと、一見、ファイルの拡張子から形式を自動判別して解凍までさせられるように見えるのだけど、zipやtarのようなArchiveと、gzipやbzip2のようなCompressorは扱いが別になっているので、一元化はしづらそう。
と、細かい話はさておき、試しにZIPを解凍するコードを書いてみた。適当に書いたのだけど、一応動いているっぽい。
続きを読む
2009年11月05日
2009年11月01日
【Commons】FileUtilsで1行のコードでファイル読み込み
あー、なんだ、上の文章を見て何かを思い出した人、最近、年取ったなぁとか思うこと多くありません?
続きを読む
2009年10月31日
【Commons】MapListHandlerで簡単にSQLの結果を取り扱う
続きを読む
2009年07月25日
2009年07月13日
2009年07月12日
2009年07月10日
2009年07月09日
【1日1Commons】BooleanUtilsのxor
// Commons Lang 2.4
// org.apache.commons.lang.BooleanUtils.xor
// boolean配列の排他的論理和
// tag boolean xor
続きを読む
2009年07月07日
【1日1Commons】booleanをintやStringに変換
// Commons Lang 2.4
// Booleanを数字に変換する
// org.apache.commons.lang.BooleanUtils
// tag boolean toInteger toString
続きを読む
【1日1Commons】intやStringをbooleanに変換
// Commons Lang 2.4
// いろんなものをBooleanに変換する
// org.apache.commons.lang.BooleanUtils.toBoolean
// tag boolean
続きを読む
2009年07月06日
【1日1Commons】Booleanからプリミティブなbooleanへ
// Commons Lang 2.4
// Boolean型を元にtrue/falseを判定する(nullチェック付き)
// org.apache.commons.lang.BooleanUtils
// tag boolean
続きを読む
2009年07月05日
【1日1Commons】イニシャルを取得
// Commons Lang 2.4
// イニシャルを抽出する
// org.apache.commons.lang.WordUtils
// tag initial
続きを読む
2009年07月04日
【1日1Commons】ちょっとだけ高機能なキャピタライズ
// Commons Lang 2.4
// キャピタライズ
// org.apache.commons.lang.WordUtils
// tag capitalize 大文字 小文字
続きを読む
2009年07月02日
【1日1Commons】ランダムな文字列を生成する
// Commons Lang 2.4
// ランダムな文字列を生成する
// org.apache.commons.lang.RandomStringUtils
// tag random string
続きを読む
2009年06月30日
【1日1Commons】HTML,Javascript,XMLのエスケープ
// Commons Lang 2.4
// HTML,Javascript,XMLのエスケープ
// org.apache.commons.lang.StringEscapeUtils
// tag エスケープ,サニタイズ
続きを読む
【1日1Commons】CSV用の値のエスケープ
// Commons Lang 2.4
// CSV用エスケープ
// org.apache.commons.lang.StringEscapeUtils
// tag エスケープ
続きを読む
2009年06月28日
【1日1Commons】実行中のOSを判定する
// Commons Lang 2.4
// 実行しているOSを判定する
// WindowsかLinuxかを判定する
// org.apache.commons.lang.SystemUtils
// tag system
// WindowsXPで動かしています
System.out.println( SystemUtils.OS_ARCH ); //=> x86
System.out.println( SystemUtils.OS_NAME ); //=> Windows XP
System.out.println( SystemUtils.OS_VERSION ); //=> 5.1
// Windowsかどうか判定
System.out.println( SystemUtils.IS_OS_WINDOWS );
// Linuxかどうか判定
System.out.println( SystemUtils.IS_OS_LINUX );
// Unixかどうか判定
// IS_OS_AIX || IS_OS_HP_UX || IS_OS_IRIX || IS_OS_LINUX ||
// IS_OS_MAC_OSX || IS_OS_SOLARIS || IS_OS_SUN_OS
// という判定をしているらしい
// MacOSの]もちゃんと入ってる
System.out.println( SystemUtils.IS_OS_UNIX );
// Macかどうか判定
System.out.println( SystemUtils.IS_OS_MAC );
【1日1Commons】実行中のJavaのバージョンを取得する
// Commons Lang 2.4
// 使用中のJVMのバージョンを判定する
// org.apache.commons.lang.SystemUtils
// system
// バージョンを取得する
System.out.println( SystemUtils.JAVA_VERSION ); //=> 1.6.0_07
// JavaDoc
// http://commons.apache.org/lang/api-release/org/apache/commons/lang/SystemUtils.html#JAVA_VERSION
// まぁ、バージョン取るだけならこれで良いのですが
System.out.println( System.getProperty("java.version") ); //=> 1.6.0_07
// いきなりFloatやintで取れたりもします
float fVersion = SystemUtils.JAVA_VERSION_FLOAT; //=> 1.6
int iVersion = SystemUtils.JAVA_VERSION_INT; //=> 160
// バージョン判定をするメソッドもいます
if( SystemUtils.IS_JAVA_1_6 ) {
System.out.println( "1.6の処理" );
}
else if ( SystemUtils.IS_JAVA_1_5 ) {
System.out.println( "1.5の処理" );
}
// JavaDoc
// http://commons.apache.org/lang/api-release/org/apache/commons/lang/SystemUtils.html#IS_JAVA_1_4
2009年06月27日
【1日1Commons】null、空白判定
// Commons Lang 2.4
// Nullか空文字を判定する
// org.apache.commons.lang.StringUtils
// tag 文字列,空白
// method
// StringUtils.isEmpty( String )
// 引数がnullか空文字ならtrueを返す
// method
// StringUtils.isBlank( string )
// 引数がnullか空文字,もしくは空白文字のみであればtureを返す
// isEmpty → null => true
StringUtils.isEmpty( null );
// isEmpty → 空文字 => true
StringUtils.isEmpty( "" );
// isEmpty → 半角スペース => false
StringUtils.isEmpty( " " );
// isEmpty → abc => false
StringUtils.isEmpty( "abc" );
// isBlank → null => true
StringUtils.isBlank( null );
// isBlank → 空文字 => true
StringUtils.isBlank( "" );
// isBlank → 半角スペース => true
StringUtils.isBlank( " " );
// isBlank → タブ => true
StringUtils.isBlank( "\t" );
// isBlank → 改行 => true
StringUtils.isBlank( "\r\n");
// isBlank → 全角スペース => true
StringUtils.isBlank( " " );
// isBlank → abc => false
StringUtils.isBlank( "abc" );
// 全角スペースもtrueになるらしい(知らなかった)
// 内部的にはCharacter.isWhitespaceを使用して空白判定している
// isWhitespace
// http://java.sun.com/javase/ja/6/docs/ja/api/java/lang/Character.html#isWhitespace(int)