001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.DbFile
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util;
023:
024: import org.apache.derby.iapi.error.StandardException;
025: import org.apache.derby.iapi.store.access.FileResource;
026: import java.io.BufferedInputStream;
027: import java.io.BufferedReader;
028: import java.io.File;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.io.OutputStream;
034: import java.lang.StringBuffer;
035: import java.net.URL;
036:
037: /**
038: Utility class for testing files stored in the database.
039: */
040: public class DbFile {
041: /**
042: Read the current generation of a file stored in the
043: database we are connected to and return a 1 line string
044: representation of the file.
045:
046: Sample usage
047: values org.apache.derbyTesting.functionTests.util.DbFile::readAsString('S1','J1');
048: @exception Exception Oops.
049: */
050: /*
051: CANT USE JarAccess - not a public API (actually it's gone!)
052: public static String
053: readAsString(String schemaName, String sqlName)
054: throws Exception
055: {
056: InputStream is = JarAccess.getAsStream(schemaName,
057: sqlName,
058: FileResource.CURRENT_GENERATION_ID);
059: return stringFromFile(is);
060: }
061: */
062: /**
063: Create a string that contains a representation of the content of
064: a file for testing.
065: @exception Exception Oops.
066: */
067: public static String stringFromFile(InputStream is)
068: throws Exception {
069: InputStreamReader isr = new InputStreamReader(is);
070: BufferedReader br = new BufferedReader(isr);
071: StringBuffer sb = new StringBuffer();
072: String l;
073: while ((l = br.readLine()) != null) {
074: sb.append(l);
075: sb.append("<CR>");
076: }
077: is.close();
078: return sb.toString();
079: }
080:
081: /**
082: Get the URL for a resource.
083:
084: @param packageName the name of the resource package
085: @param name the name of the resourse.
086: */
087: public static URL getResourceURL(String packageName, String name) {
088: String resourceName = "/" + packageName.replace('.', '/') + "/"
089: + name;
090: //
091: //We need a class to get our URL. Since we give a
092: //fully qualified name for the URL, any class will
093: //do.
094: Class c = resourceName.getClass();
095: URL url = c.getResource(resourceName);
096: return url;
097: }
098:
099: /**
100: Get an InputStream for reading a resource.
101:
102: @param packageName the name of the resource package
103: @param name the name of the resourse.
104: @exception Exception Oops.
105: */
106: public static InputStream getResourceAsStream(String packageName,
107: String name) {
108: String resourceName = "/" + packageName.replace('.', '/') + "/"
109: + name;
110: //
111: //We need a class to get our URL. Since we give a
112: //fully qualified name for the URL, any class will
113: //do.
114: Class c = resourceName.getClass();
115: InputStream result = c.getResourceAsStream(resourceName);
116: return result;
117: }
118:
119: public static boolean deleteFile(String outputFileName)
120: throws Exception {
121: File f = new File(outputFileName);
122:
123: return f.delete();
124: }
125:
126: public static String mkFileFromResource(String packageName,
127: String resourceName) throws Exception {
128: return mkFileFromResource(packageName, resourceName,
129: resourceName);
130: }
131:
132: public static String mkFileFromResource(String packageName,
133: String resourceName, String outputFileName)
134: throws Exception {
135: File f = new File(outputFileName);
136: InputStream is = getResourceAsStream(packageName, resourceName);
137: BufferedInputStream bis = new BufferedInputStream(is);
138: OutputStream os = new FileOutputStream(f);
139: byte[] buf = new byte[4096];
140: int readThisTime = 0;
141: while ((readThisTime = bis.read(buf)) != -1)
142: os.write(buf, 0, readThisTime);
143: os.close();
144: return f.getAbsolutePath();
145: }
146: }
|