スキップしてメイン コンテンツに移動

投稿

Java File Copy - Guava, Java7, Java Legacy, FileChannel, CommonsIO

There are a lot of way to copy file in Java. I have wrote sample codes for them. I think Java 7 way will be the normal & standard way soon!! import static com.dukesoftware.utils.common.ExceptionUtils.throwNotImplementedException; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.net.URI; import java.nio.channels.FileChannel; import java.nio.file.Paths; import org.apache.commons.io.FileUtils; import com.google.common.io.Closeables; import com.google.common.io.Files; public enum Copier { GUAVA{ @Override public void copy(File src, File dest) throws IOException { Files.copy(src, dest); } }, JAVA7{ @

Java: Auto Resize Image Canvas (Swing)

I wrote an image canvas class which can be automatically re-size image so that it fits the window. The code itself is pretty simple. package com.dukesoftware.utils.swing.others; import java.awt.Graphics; import java.awt.Image; import javax.swing.JPanel; public class AutoResizeImageCanvas extends JPanel{ private Image img; public void setImage(Image img){ this.img = img; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); final int panelWidth = getWidth(); final int panelHeight = getHeight(); g.fillRect(0, 0, panelWidth, panelHeight); if(img != null){ final int imgWidth = img.getWidth(null); final int imgHeight = img.getHeight(null); final double rW = (double)panelWidth / imgWidth; final double rH = (double)panelHeight / imgHeight; int newWidth; int newHeight; if(rW < rH){

Ant Build Script for ActionScript 3.0 (AIR Application)

This is my ant build script used for building app. You know FlashDevelop can build swf however it only compiles minimum classes directly used in the program. So I always run ant build script in order to compile all ".as" and ".mxml" source files If you would like to use my ant script, need some preparation.... Flex PMD: You can download Flex PMD, which is used in my ant script, from here . PMD xslt: I just downloaded official sourceforge PMD zip (the version is 5.0.0 when I wrote this article) and simply picked up some useful xslt from pmd-src-5.0.0/etc/xslt My project directory hierarchy is something like this... <?xml version="1.0" encoding="utf-8" ?> <project name="DukeSoftwareBuildAS3" default="all" basedir="."> <property name="root.dir" value=".." /> <property file="${root.dir}/proj.properties" /> <property file="build.prop

ActionScript 3.0: Resize Image

Main flow part code. package utils.tool { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import utils.ImageUtils; public class ImageResizer { private var maxW:int, maxH:int; private var smoothing:Boolean; private var saveFunction:Function; public function ImageResizer(maxW:int, maxH:int, smoothing:Boolean = false, type:String="jpg") { this.maxW = maxW; this.maxH = maxH; this.smoothing = smoothing; if (type === "png") { saveFunction = ImageUtils.saveBitmapDataAsPNGAsync; } else if(type === "jpg"){ saveFunction = ImageUtils.saveBitmapDataAsJPEGAsync; } else { throw new Error("Not Supported"); } }

ActionScript 3.0: Save BitmapData As JPEG or PNG

public static function saveBitmapDataAsJPEG(path:String, bitmapData:BitmapData, quality:Number=50.0):void { saveByteData(new JPEGEncoder(quality).encode(bitmapData), path); } public static function saveBitmapDataAsJPEGAsync(path:String, bitmapData:BitmapData, quality:Number=50.0):void { saveByteDataAsync(new JPEGEncoder(quality).encode(bitmapData), path); } public static function saveBitmapDataAsPNG(path:String, bitmapData:BitmapData):void { saveByteData(new PNGEncoder().encode(bitmapData), path); } public static function saveBitmapDataAsPNGAsync(path:String, bitmapData:BitmapData):void { saveByteDataAsync(new PNGEncoder().encode(bitmapData), path); } Here is my IO Utility methods. public static function saveByteData(data:ByteArray, path:String):void { try { var file:File = new File(path); var fs:FileStream = new FileStream(); fs.open(file, FileMode.WRITE); fs.writeBytes(data); fs.close(); } catch (err:IOError) { trace(err); } } public static function saveB

JavaのID3タグ解析ライブラリ

Javaでmp3ファイルのID3タグを解析するライブラリを色々と調べてみました。参考になれば幸いです。 以下に挙げていくコードは、特に意味のあることをしている訳ではありませんが、 どうやってタグの読み込み、書き換えを行えるかというサンプルとして参考にして頂ければと思います。 MyID3: a Java ID3 Tag Library import java.io.File; import java.io.IOException; import org.cmc.music.common.MusicMetadata; import org.cmc.music.myid3.MusicMetadataSet; import org.cmc.music.myid3.MyID3; /** * {@link http://www.fightingquaker.com/myid3/} */ public class MyID3Tester { public static void main(String[] args) throws IOException { File mp3File = new File("C:\\temp\\music.mp3"); MyID3 id3 = new MyID3(); MusicMetadataSet src_set = id3.read(mp3File); // read metadata if (src_set == null){ System.out.println("could not read data"); return; } // You can extract simplified information MusicMetadata metadata = src_set.getSimplified(); System.out.println(metadata.getArtist()); System.out.println(metadata.getAlbum()); // this doesn't work for me somehow :( System.out.println(metad

Java: Get Available Font on AWT environment

If you would like to check available font on Java environment, you should use this method. GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); The following code is utility class for Font. package com.dukesoftware.utils.common; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.util.Arrays; public class FontUtils { public static void main(String[] args) { printAvailableFonts(); } public static void printAvailableFonts() { System.out.println(Arrays.toString(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames())); } public static boolean isAvailableFont(String font){ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); final String[] fontNames = ge.getAvailableFontFamilyNames(); for(String name: fontNames){ if(name.equals(font)){ return true; } } return false;