1 25 26 29 package net.killingar.forum.comics; 30 31 import net.killingar.forum.internal.Comic; 32 import net.killingar.forum.internal.Strip; 33 import net.killingar.forum.internal.managers.ComicManager; 34 35 import java.io.*; 36 import java.net.URL ; 37 import java.net.URLConnection ; 38 import java.util.ArrayList ; 39 import java.util.List ; 40 41 public class ComicsSaver 42 { 43 47 public static void saveAll(ComicManager cmgr, File directory, String urlPrefix, Writer out) throws Exception 48 { 49 Comic comics[] = cmgr.getComics(); 50 List threads = new ArrayList (); 51 for (int i = 0; i < comics.length; i++) 52 { 53 Thread t = save(cmgr, new File(directory, Long.toString(comics[i].ID)), urlPrefix, comics[i].ID, out); 54 if (t != null) 55 threads.add(t); 56 } 57 58 for (int i = 0; i < threads.size(); i++) 59 ((Thread )threads.get(i)).join(); 60 61 if (out != null)out.flush(); 62 } 63 64 public static Thread save(ComicManager cmgr, File directory, String urlPrefix, long comicID, Writer out) 65 { 66 Thread t = new Thread (new Saver(cmgr, directory, comicID, urlPrefix, out)); 67 68 t.setPriority(t.getPriority()-1); 69 70 t.start(); 71 72 return t; 73 } 74 75 public static class Saver implements Runnable 76 { 77 private ComicManager cmgr; 78 private File directory; 79 private long comicID; 80 private String urlPrefix; 81 private Writer out; 82 83 public Saver(ComicManager _cmgr, File _directory, long _comicID, String _urlPrefix, Writer _out) 84 { 85 cmgr = _cmgr; 86 directory = _directory; 87 comicID = _comicID; 88 urlPrefix = _urlPrefix; 89 out = _out; 90 } 91 92 public void run() 93 { 94 try 95 { 96 directory.mkdirs(); 97 98 Comic comic = cmgr.getComic(comicID); 99 if (out != null)out.write("{ "+comic.name+"\n"); 100 101 Strip strips[] = cmgr.getStrips(comicID); 102 if (out != null)out.write(" "+strips.length+" "+comic.name); 103 for (int i = 0; i < strips.length; i++) 104 { 105 try 107 { 108 URLConnection con = new URL (strips[i].localURL).openConnection(); 109 con.connect(); 110 111 continue; 112 } 113 catch (Exception e) 114 { 115 } 116 117 String filename = strips[i].time.toString(); 119 String url = strips[i].URL; 120 121 try 123 { 124 URLConnection con = new URL (url).openConnection(); 125 con.connect(); 126 127 String contentType = con.getContentType(); 129 if (contentType.equals("image/gif")) 130 filename = filename+".gif"; 131 else if (contentType.equals("image/jpeg")) 132 filename = filename+".jpg"; 133 else if (contentType.equals("image/png")) 134 filename = filename+".png"; 135 else 136 throw new IOException("unknown content type"); 137 139 File localCopy = new File(directory, filename); 140 141 if (localCopy.exists())continue; 142 143 InputStream in = con.getInputStream(); 144 OutputStream outstream = new FileOutputStream(localCopy); 145 byte buf[] = new byte[524288]; 146 int read, totalRead = 0; 147 148 while(true) 149 { 150 read = in.read(buf); 151 if (read == -1)break; 152 outstream.write(buf, 0, read); 153 totalRead += read; 154 } 155 outstream.close(); 156 in.close(); 157 if (out != null)out.write("! "); 159 } 160 catch (IOException e) 161 { 162 out.write(e.toString()); 163 e.printStackTrace(); 164 if (out != null)out.write("x "); 166 } 167 168 strips[i].localURL = urlPrefix+comicID+"/"+filename; 169 cmgr.changeStrip(strips[i]); 170 } 171 if (out != null)out.write("} "+comic.name); 172 } 173 catch (Exception e){} 174 } 175 } 176 } | Popular Tags |