最近因為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年11月8日 星期六
2008年6月12日 星期四
simple byte-oriented file copy
- /**
- * simple copy image,
- * @param src
- * @param dest
- */
- byte[] buffer = new byte[1024];
- FileInputStream r = null;
- FileOutputStream w = null;
- int r_num=0;
- try{
- r_num = r.read(buffer, 0, 1024);
- while(r_num != -1){
- w.write(buffer, 0, r_num);
- r_num = r.read(buffer, 0, 1024);
- }
- }
- e.printStackTrace();
- }
- finally{
- try{
- if(r != null)
- r.close();
- if(w != null)
- w.close();
- }
- ex.printStackTrace();
- }
- }
- }
可用於任何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的動作就是相反.
compression 的infile就是原先的tar起來的file, outfile就是compression放置的地方
decompression的動作就是相反.
- /**
- * the input is the file that we want to compression
- * the output is the compressed file
- * @param infile
- * @param outfile
- * @return
- * @throws IOException
- */
- BufferedInputStream fin = null;
- BufferedOutputStream fout = null;
- try{
- byte[] buffer = new byte[1024];
- int offset = -1;
- while( (offset = fin.read(buffer)) != -1){
- zout.write(buffer, 0, offset);
- }
- zout.closeEntry();
- byte[] compressed = out.toByteArray();
- zout.close();
- fout.write(compressed);
- fout.close();
- }
- }
- finally{
- if(fin != null){
- fin.close();
- }
- if(fout != null){
- fout.flush();
- fout.close();
- }
- }
- }
- /**
- * the input is the compressed file
- * the output is the original file
- * @param compressed
- * @return
- * @throws IOException
- */
- int read=0;
- byte[] buffer = new byte[1024];
- BufferedInputStream fin = null;
- BufferedWriter fout = null;
- ZipInputStream zin = null;
- ByteArrayOutputStream out = null;
- try{
- int offset = -1;
- while((offset = zin.read(buffer)) != -1) {
- out.write(buffer, 0, offset);
- }
- fout.write(out.toString());
- }
- }
- finally{
- if(fin != null)
- fin.close();
- if(fout != null){
- fout.flush();
- fout.close();
- }
- if(out != null)
- out.close();
- if(zin != null)
- zin.close();
- }
- }
訂閱:
文章 (Atom)