将数据写入PNG文件并保证png能正常浏览

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;


public class PNGEncoder {
 /**
  * @author qpaber
  * PNG解码类 用于将数据写入PNG文件
  */
 private static final byte[] _PNGEND = {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,
             (byte)0x49,(byte)0x45,(byte)0x4E,(byte)0x44};
 
 /**
  * 将字符串写入PNG文件
  * @param f PNG文件
  * @param str 需要写入的文本信息
  */
 public static void writePNG(File f,String str){
  RandomAccessFile raf = null;
  FileInputStream tmpIn = null;
  FileOutputStream tmpOut = null;
  try{
   raf = new RandomAccessFile(f, "rw");
   File tmp = File.createTempFile("tmp", null);
   tmpIn = new FileInputStream(tmp);
   tmpOut = new FileOutputStream(tmp);
   
   //循环将PNG信息写入临时文件
   byte[] bbuf = new byte[12];
   int hasRead = 0;
   int i=0;
   raf.seek(0);
   while((hasRead = raf.read(bbuf))>0){
    if(isEND(bbuf)){
     tmpOut.write(bbuf,0,hasRead);
     break;
    }else{
     tmpOut.write(bbuf,0,1);
    }
    i=i+1;
    raf.seek(i);
   }
   //将字符串追加到临时文件
   tmpOut.write(str.getBytes());
   //将临时文件内容写入PNG文件
   raf.close();
   f.delete();
   raf = new RandomAccessFile(f, "rw");
   bbuf = new byte[1024];
   raf.seek(0);
   while((hasRead = tmpIn.read(bbuf)) > 0){
    raf.write(bbuf,0,hasRead);
   }
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   try{
    if(raf!=null){
     raf.close();
     raf=null;
    }
    if(tmpIn!=null){
     tmpIn.close();
     tmpIn=null;
    }
    if(tmpOut!=null){
     tmpOut.close();
     tmpOut=null;
    }
   }catch(Exception e){
    e.printStackTrace();
   }
  }
 }
 /**
  * 是否为结束符
  * @param bbuf 需判断的byte[]
  * @return 是 true
  */
 private static boolean isEND(byte[] bbuf){
  if(bbuf[0] == _PNGEND[0] && bbuf[1] == _PNGEND[1] && bbuf[2] == _PNGEND[2] && bbuf[3] == _PNGEND[3] && 
    bbuf[4] == _PNGEND[4] && bbuf[5] == _PNGEND[5] && bbuf[6] == _PNGEND[6] && bbuf[7] == _PNGEND[7]){
   return true;
  }
  return false;
 }
 
 public static void main(String[] args){
  writePNG(new File("12.png"),"wkwwkwkwkwkwkwkwkwkwkwkwkwkwkwkwk");
  System.out.println("ok");
 }
}

posted @ 2012-11-07 08:29  nzlov  阅读(1282)  评论(0编辑  收藏  举报