顯示具有 java 標籤的文章。 顯示所有文章
顯示具有 java 標籤的文章。 顯示所有文章

2008年11月8日 星期六

Crawling Data in Amazon.com

最近因為research會用到music data,所以就利用Amazon提供的一個Web services(AWS) 。
第一步當然就先註冊一個userid,然後再看Amazon提供的programmer guide就可以抓data了。
不過有幾點事情比較有趣...主要的原因是因為需要music的content data,而Amazon所能提供的只有大概30秒左右的sample。當然,我還是把sample的曲目爬下來了...Orz(不知道有沒有違反著作權..不過我也只是做研究沒有商業用途)。
不過有趣的地方在於要找到某個mp3的原始url是一個耗時的地方(1. 從amazon抓html本身就耗時了; 2.要parse html找到正確的mp3 location也是個耗時的地方),所以就模擬了HTTP 1.1的作法...XD
先把系統預設(daemon thread)有10個可以跑的thread,然後每當要抓mp3 data的時候就叫醒一個thread(稱為mp3 thread)。 mp3thread只有一個動作就是抓mp3的url, 再利用stream的方式把mp3 download,然後繼續回去睡覺。daemon thread如果發現目前10個mp3thread都有再執行的話,就等待直到有其他mp3thread可以被使用為止。概念很簡單,不過卻是一個很基本的利用java做synchronize的問題。
不過mp3thread太多就會被Amazon reject掉,因為太短時間內有太多的request了..Orz

2008年6月12日 星期四

simple byte-oriented file copy

  1. /**
  2. * simple copy image,
  3. * @param src
  4. * @param dest
  5. */
  6. public void copyImage(File src, File dest){
  7. byte[] buffer = new byte[1024];
  8. FileInputStream r = null;
  9. int r_num=0;
  10. try{
  11. r = new FileInputStream( src);
  12. w = new FileOutputStream( dest);
  13. r_num = r.read(buffer, 0, 1024);
  14. while(r_num != -1){
  15. w.write(buffer, 0, r_num);
  16. r_num = r.read(buffer, 0, 1024);
  17. }
  18. }
  19. catch(IOException e){
  20. e.printStackTrace();
  21. }
  22. finally{
  23. try{
  24. if(r != null)
  25. r.close();
  26. if(w != null)
  27. w.close();
  28. }
  29. catch(IOException ex){
  30. ex.printStackTrace();
  31. }
  32. }
  33. }

可用於任何byte-oriented file, ex: image

2008年5月27日 星期二

簡易的java compression and decompression

目前在做之前學長的Video Annotation tool多個model的整合...常常training出來的model感覺檔案都很大...(因為我只有把它tar起來XD)...有鑑於此,,所以寫了一個簡易的compression/decompression的程式..主要改自Simple String Compression Functions 不過為了符合我的需要,input/output都是一個file,,不過這個blog好像沒辦法上傳檔案..XD所以底下大概看一下吧XD

compression 的infile就是原先的tar起來的file, outfile就是compression放置的地方
decompression的動作就是相反.

  1. /**
  2. * the input is the file that we want to compression
  3. * the output is the compressed file
  4. * @param infile
  5. * @param outfile
  6. * @return
  7. * @throws IOException
  8. */
  9. static public void compression(String infile, String outfile)throws IOException{
  10. try{
  11. byte[] buffer = new byte[1024];
  12. int offset = -1;
  13. fin = new BufferedInputStream( new FileInputStream(infile));
  14. fout = new BufferedOutputStream ( new FileOutputStream(outfile));
  15. zout.putNextEntry(new ZipEntry("0"));
  16. while( (offset = fin.read(buffer)) != -1){
  17. zout.write(buffer, 0, offset);
  18. }
  19. zout.closeEntry();
  20. byte[] compressed = out.toByteArray();
  21. zout.close();
  22. fout.write(compressed);
  23. fout.close();
  24. }
  25. catch(IOException e){
  26. new IOException("simpleCompression.compression exception");
  27. }
  28. finally{
  29. if(fin != null){
  30. fin.close();
  31. }
  32. if(fout != null){
  33. fout.flush();
  34. fout.close();
  35. }
  36. }
  37. }
  38. /**
  39. * the input is the compressed file
  40. * the output is the original file
  41. * @param compressed
  42. * @return
  43. * @throws IOException
  44. */
  45. static public void decompression(String infile, String outfile)throws IOException{
  46. int read=0;
  47. byte[] buffer = new byte[1024];
  48. BufferedWriter fout = null;
  49. ZipInputStream zin = null;
  50. try{
  51. fin = new BufferedInputStream( new FileInputStream(infile));
  52. zin = new ZipInputStream(fin);
  53. ZipEntry entry = zin.getNextEntry();
  54. int offset = -1;
  55. while((offset = zin.read(buffer)) != -1) {
  56. out.write(buffer, 0, offset);
  57. }
  58. fout = new BufferedWriter(new FileWriter(outfile));
  59. fout.write(out.toString());
  60. }
  61. catch(IOException e){
  62. new IOException("simpleCompression.decompression exception");
  63. }
  64. finally{
  65. if(fin != null)
  66. fin.close();
  67. if(fout != null){
  68. fout.flush();
  69. fout.close();
  70. }
  71. if(out != null)
  72. out.close();
  73. if(zin != null)
  74. zin.close();
  75. }
  76. }