KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > comics > ComicsSaver


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Automates local cashing of comics.
28  */

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 JavaDoc;
37 import java.net.URLConnection JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40
41 public class ComicsSaver
42 {
43     /**
44      * Save all unsaved strips to the specified directory.
45      * Strips are saved under a directory with the same ID number as the comic it belongs to.
46      */

47     public static void saveAll(ComicManager cmgr, File directory, String JavaDoc urlPrefix, Writer out) throws Exception JavaDoc
48     {
49         Comic comics[] = cmgr.getComics();
50         List JavaDoc threads = new ArrayList JavaDoc();
51         for (int i = 0; i < comics.length; i++)
52         {
53             Thread JavaDoc 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 JavaDoc)threads.get(i)).join();
60
61         if (out != null)out.flush();
62     }
63
64     public static Thread JavaDoc save(ComicManager cmgr, File directory, String JavaDoc urlPrefix, long comicID, Writer out)
65     {
66         Thread JavaDoc t = new Thread JavaDoc(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 JavaDoc
76     {
77         private ComicManager cmgr;
78         private File directory;
79         private long comicID;
80         private String JavaDoc urlPrefix;
81         private Writer out;
82
83         public Saver(ComicManager _cmgr, File _directory, long _comicID, String JavaDoc _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                     // check if the local copy exists, if it does, don't continue further
106
try
107                     {
108                         URLConnection JavaDoc con = new URL JavaDoc(strips[i].localURL).openConnection();
109                         con.connect();
110
111                         continue;
112                     }
113                     catch (Exception JavaDoc e)
114                     {
115                     }
116
117                     // get on with it
118
String JavaDoc filename = strips[i].time.toString();
119                     String JavaDoc url = strips[i].URL;
120
121                     // download picture
122
try
123                     {
124                         URLConnection JavaDoc con = new URL JavaDoc(url).openConnection();
125                         con.connect();
126
127                         // get content-type, and add an appropriate extension to the filename
128
String JavaDoc 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                             //filename = filename+".unknown";
138

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                         //out.write(" download complete on "+filename+"\n");
158
if (out != null)out.write("! ");
159                     }
160                     catch (IOException e)
161                     {
162                         out.write(e.toString());
163                         e.printStackTrace();
164                         //out.write(" download failed on "+url+"\n");
165
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 JavaDoc e){}
174         }
175     }
176 }
Popular Tags