source: other-projects/trunk/realistic-books/packages/AntInstaller/web/java2html/antinstaller/org/tp23/antinstaller/selfextract/ResourceExtractor.java.html@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

File size: 7.7 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
2<HTML>
3<HEAD>
4<LINK REL=STYLESHEET TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style">
5<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.4">
6<TITLE>org.tp23.antinstaller.selfextract.ResourceExtractor (Java2HTML)</TITLE>
7</HEAD>
8<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
9<tr>
10<td colspan="2" width="33%">&nbsp;</td>
11<td align="center" colspan="2" width="33%">
12<font size="4" class="ClassName">ResourceExtractor.java</font>
13</td>
14<td align="right" colspan="2" width="33%">&nbsp;</td>
15</tr>
16</TABLE>
17<pre CLASS="Classes">
18<FONT CLASS="LN">1 </FONT><A NAME="1"></A><FONT CLASS="MultiLineComment">/*
19<FONT CLASS="LN">2 </FONT><A NAME="2"></A> * Copyright 2005 Paul Hinds
20<FONT CLASS="LN">3 </FONT><A NAME="3"></A> *
21<FONT CLASS="LN">4 </FONT><A NAME="4"></A> * Licensed under the Apache License, Version 2.0 (the "License");
22<FONT CLASS="LN">5 </FONT><A NAME="5"></A> * you may not use this file except in compliance with the License.
23<FONT CLASS="LN">6 </FONT><A NAME="6"></A> * You may obtain a copy of the License at
24<FONT CLASS="LN">7 </FONT><A NAME="7"></A> *
25<FONT CLASS="LN">8 </FONT><A NAME="8"></A> * http://www.apache.org/licenses/LICENSE-2.0
26<FONT CLASS="LN">9 </FONT><A NAME="9"></A> *
27<FONT CLASS="LN">10</FONT><A NAME="10"></A> * Unless required by applicable law or agreed to in writing, software
28<FONT CLASS="LN">11</FONT><A NAME="11"></A> * distributed under the License is distributed on an "AS IS" BASIS,
29<FONT CLASS="LN">12</FONT><A NAME="12"></A> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30<FONT CLASS="LN">13</FONT><A NAME="13"></A> * See the License for the specific language governing permissions and
31<FONT CLASS="LN">14</FONT><A NAME="14"></A> * limitations under the License.
32<FONT CLASS="LN">15</FONT><A NAME="15"></A> */</FONT>
33<FONT CLASS="LN">16</FONT><A NAME="16"></A><FONT CLASS="Package">package</FONT> <A HREF="../../../../org.tp23.antinstaller.selfextract.index.html" target="packageFrame">org.tp23.antinstaller.selfextract</A>;
34<FONT CLASS="LN">17</FONT><A NAME="17"></A>
35<FONT CLASS="LN">18</FONT><A NAME="18"></A><FONT CLASS="Import">import</FONT> java.io.BufferedReader;
36<FONT CLASS="LN">19</FONT><A NAME="19"></A><FONT CLASS="Import">import</FONT> java.io.File;
37<FONT CLASS="LN">20</FONT><A NAME="20"></A><FONT CLASS="Import">import</FONT> java.io.FileOutputStream;
38<FONT CLASS="LN">21</FONT><A NAME="21"></A><FONT CLASS="Import">import</FONT> java.io.IOException;
39<FONT CLASS="LN">22</FONT><A NAME="22"></A><FONT CLASS="Import">import</FONT> java.io.InputStream;
40<FONT CLASS="LN">23</FONT><A NAME="23"></A><FONT CLASS="Import">import</FONT> java.io.InputStreamReader;
41<FONT CLASS="LN">24</FONT><A NAME="24"></A><FONT CLASS="FormalComment">/**
42<FONT CLASS="LN">25</FONT><A NAME="25"></A> * Copies resources from the Classpath to the filesystem replacing regex with replacement
43<FONT CLASS="LN">26</FONT><A NAME="26"></A> * and correcting line endings
44<FONT CLASS="LN">27</FONT><A NAME="27"></A> * @author teknopaul
45<FONT CLASS="LN">28</FONT><A NAME="28"></A> *
46<FONT CLASS="LN">29</FONT><A NAME="29"></A> */</FONT>
47<FONT CLASS="LN">30</FONT><A NAME="30"></A><FONT CLASS="Public">public</FONT> <FONT CLASS="Class">class</FONT> ResourceExtractor {
48<FONT CLASS="LN">31</FONT><A NAME="31"></A>
49<FONT CLASS="LN">32</FONT><A NAME="32"></A> <FONT CLASS="FormalComment">/**
50<FONT CLASS="LN">33</FONT><A NAME="33"></A> * @param resource Must start with / e.g. /org/tp23/myFile.txt
51<FONT CLASS="LN">34</FONT><A NAME="34"></A> * @param dest
52<FONT CLASS="LN">35</FONT><A NAME="35"></A> * @throws IOException
53<FONT CLASS="LN">36</FONT><A NAME="36"></A> */</FONT>
54<FONT CLASS="LN">37</FONT><A NAME="37"></A> <FONT CLASS="Public">public</FONT> <FONT CLASS="Void">void</FONT> copyResource(String resource, File dest, String regex, String replace) <FONT CLASS="Throws">throws</FONT> IOException{
55<FONT CLASS="LN">38</FONT><A NAME="38"></A> InputStream is = <FONT CLASS="This">this</FONT>.getClass().getResourceAsStream(resource);
56<FONT CLASS="LN">39</FONT><A NAME="39"></A> <FONT CLASS="If">if</FONT>(is == <FONT CLASS="Null">null</FONT>){
57<FONT CLASS="LN">40</FONT><A NAME="40"></A> <FONT CLASS="Throw">throw</FONT> <FONT CLASS="New">new</FONT> IOException(<FONT CLASS="StringLiteral">"Can not find resource: "</FONT> + resource);
58<FONT CLASS="LN">41</FONT><A NAME="41"></A> }
59<FONT CLASS="LN">42</FONT><A NAME="42"></A> InputStreamReader isr = <FONT CLASS="New">new</FONT> InputStreamReader(is);
60<FONT CLASS="LN">43</FONT><A NAME="43"></A> BufferedReader br = <FONT CLASS="New">new</FONT> BufferedReader(isr);
61<FONT CLASS="LN">44</FONT><A NAME="44"></A> FileOutputStream bos = <FONT CLASS="New">new</FONT> FileOutputStream(dest);
62<FONT CLASS="LN">45</FONT><A NAME="45"></A> String line = <FONT CLASS="Null">null</FONT>;
63<FONT CLASS="LN">46</FONT><A NAME="46"></A> <FONT CLASS="While">while</FONT>((line = br.readLine()) != <FONT CLASS="Null">null</FONT>){
64<FONT CLASS="LN">47</FONT><A NAME="47"></A> bos.write(line.replaceAll(regex, replace).getBytes());
65<FONT CLASS="LN">48</FONT><A NAME="48"></A> bos.write(System.getProperty(<FONT CLASS="StringLiteral">"line.separator"</FONT>).getBytes());
66<FONT CLASS="LN">49</FONT><A NAME="49"></A> }
67<FONT CLASS="LN">50</FONT><A NAME="50"></A> bos.flush();
68<FONT CLASS="LN">51</FONT><A NAME="51"></A> bos.close();
69<FONT CLASS="LN">52</FONT><A NAME="52"></A> br.close();
70<FONT CLASS="LN">53</FONT><A NAME="53"></A> }
71<FONT CLASS="LN">54</FONT><A NAME="54"></A> <FONT CLASS="FormalComment">/**
72<FONT CLASS="LN">55</FONT><A NAME="55"></A> * @param resource Must start with / e.g. /org/tp23/myFile.txt
73<FONT CLASS="LN">56</FONT><A NAME="56"></A> * @param dest
74<FONT CLASS="LN">57</FONT><A NAME="57"></A> * @throws IOException
75<FONT CLASS="LN">58</FONT><A NAME="58"></A> */</FONT>
76<FONT CLASS="LN">59</FONT><A NAME="59"></A> <FONT CLASS="Public">public</FONT> <FONT CLASS="Void">void</FONT> copyResourceBinary(String resource, File dest) <FONT CLASS="Throws">throws</FONT> IOException{
77<FONT CLASS="LN">60</FONT><A NAME="60"></A> InputStream is = <FONT CLASS="This">this</FONT>.getClass().getResourceAsStream(resource);
78<FONT CLASS="LN">61</FONT><A NAME="61"></A> <FONT CLASS="If">if</FONT>(is == <FONT CLASS="Null">null</FONT>){
79<FONT CLASS="LN">62</FONT><A NAME="62"></A> <FONT CLASS="Throw">throw</FONT> <FONT CLASS="New">new</FONT> IOException(<FONT CLASS="StringLiteral">"Can not find resource: "</FONT> + resource);
80<FONT CLASS="LN">63</FONT><A NAME="63"></A> }
81<FONT CLASS="LN">64</FONT><A NAME="64"></A> FileOutputStream bos = <FONT CLASS="New">new</FONT> FileOutputStream(dest);
82<FONT CLASS="LN">65</FONT><A NAME="65"></A> <FONT CLASS="Byte">byte</FONT>[] buffer = <FONT CLASS="New">new</FONT> <FONT CLASS="Byte">byte</FONT>[<FONT CLASS="IntegerLiteral">1024</FONT>];
83<FONT CLASS="LN">66</FONT><A NAME="66"></A> <FONT CLASS="For">for</FONT>(<FONT CLASS="Int">int</FONT> read = <FONT CLASS="IntegerLiteral">0</FONT>; (read = is.read(buffer)) &gt; <FONT CLASS="IntegerLiteral">0</FONT>; ){
84<FONT CLASS="LN">67</FONT><A NAME="67"></A> bos.write(buffer, <FONT CLASS="IntegerLiteral">0</FONT>, read);
85<FONT CLASS="LN">68</FONT><A NAME="68"></A> }
86<FONT CLASS="LN">69</FONT><A NAME="69"></A> bos.flush();
87<FONT CLASS="LN">70</FONT><A NAME="70"></A> bos.close();
88<FONT CLASS="LN">71</FONT><A NAME="71"></A> is.close();
89<FONT CLASS="LN">72</FONT><A NAME="72"></A> }
90<FONT CLASS="LN">73</FONT><A NAME="73"></A>
91<FONT CLASS="LN">74</FONT><A NAME="74"></A>}
92<FONT CLASS="LN">75</FONT><A NAME="75"></A></pre>
93</BODY>
94</HTML>
Note: See TracBrowser for help on using the repository browser.