KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > DbFile


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.DbFile
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.store.access.FileResource;
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.lang.StringBuffer JavaDoc;
35 import java.net.URL JavaDoc;
36
37 /**
38   Utility class for testing files stored in the database.
39   */

40 public class DbFile
41 {
42     /**
43       Read the current generation of a file stored in the
44       database we are connected to and return a 1 line string
45       representation of the file.
46
47       Sample usage
48       values org.apache.derbyTesting.functionTests.util.DbFile::readAsString('S1','J1');
49       @exception Exception Oops.
50       */

51 /*
52 CANT USE JarAccess - not a public API (actually it's gone!)
53 public static String
54     readAsString(String schemaName, String sqlName)
55          throws Exception
56     {
57         InputStream is = JarAccess.getAsStream(schemaName,
58                                             sqlName,
59                                             FileResource.CURRENT_GENERATION_ID);
60         return stringFromFile(is);
61     }
62 */

63     /**
64       Create a string that contains a representation of the content of
65       a file for testing.
66       @exception Exception Oops.
67       */

68     public static String JavaDoc
69     stringFromFile(InputStream JavaDoc is)
70          throws Exception JavaDoc
71     {
72         InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(is);
73         BufferedReader JavaDoc br =
74             new BufferedReader JavaDoc(isr);
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         String JavaDoc l;
77         while((l = br.readLine()) != null) {
78             sb.append(l);
79             sb.append("<CR>");
80         }
81         is.close();
82         return sb.toString();
83     }
84
85     /**
86       Get the URL for a resource.
87
88       @param packageName the name of the resource package
89       @param name the name of the resourse.
90       */

91     public static URL JavaDoc
92     getResourceURL(String JavaDoc packageName, String JavaDoc name)
93     {
94         String JavaDoc resourceName =
95             "/"+
96             packageName.replace('.','/')+
97             "/"+
98             name;
99         //
100
//We need a class to get our URL. Since we give a
101
//fully qualified name for the URL, any class will
102
//do.
103
Class JavaDoc c = resourceName.getClass();
104         URL JavaDoc url = c.getResource(resourceName);
105         return url;
106     }
107
108     /**
109       Get an InputStream for reading a resource.
110
111       @param packageName the name of the resource package
112       @param name the name of the resourse.
113       @exception Exception Oops.
114       */

115     public static InputStream JavaDoc
116     getResourceAsStream(String JavaDoc packageName, String JavaDoc name)
117     {
118         String JavaDoc resourceName =
119             "/"+
120             packageName.replace('.','/')+
121             "/"+
122             name;
123         //
124
//We need a class to get our URL. Since we give a
125
//fully qualified name for the URL, any class will
126
//do.
127
Class JavaDoc c = resourceName.getClass();
128         InputStream JavaDoc result = c.getResourceAsStream(resourceName);
129         return result;
130     }
131
132     public static boolean deleteFile( String JavaDoc outputFileName )
133          throws Exception JavaDoc
134     {
135         File JavaDoc f = new File JavaDoc( outputFileName );
136
137         return f.delete();
138     }
139
140     public static String JavaDoc mkFileFromResource
141     (String JavaDoc packageName, String JavaDoc resourceName)
142          throws Exception JavaDoc
143     {
144         return mkFileFromResource( packageName, resourceName, resourceName );
145     }
146
147     public static String JavaDoc mkFileFromResource
148     ( String JavaDoc packageName, String JavaDoc resourceName, String JavaDoc outputFileName )
149          throws Exception JavaDoc
150     {
151         File JavaDoc f = new File JavaDoc( outputFileName );
152         InputStream JavaDoc is = getResourceAsStream(packageName,resourceName);
153         BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is);
154         OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
155         byte[]buf=new byte[4096];
156         int readThisTime = 0;
157         while((readThisTime = bis.read(buf)) != -1)
158             os.write(buf,0,readThisTime);
159         os.close();
160         return f.getAbsolutePath();
161     }
162 }
163  
164
Popular Tags