1 19 20 package org.netbeans.api.java.source; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.EOFException ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.io.OutputStreamWriter ; 31 import java.io.Writer ; 32 import java.net.URL ; 33 import java.util.zip.GZIPInputStream ; 34 import org.netbeans.api.java.source.query.ResultTableModel; 35 import org.netbeans.api.java.source.transform.Transformer; 36 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.filesystems.Repository; 40 41 47 public final class TestUtilities { 48 49 private TestUtilities() {} 51 52 176 public final static String copyFileToString (java.io.File f) throws java.io.IOException { 177 int s = (int)f.length (); 178 byte[] data = new byte[s]; 179 int len = new FileInputStream (f).read (data); 180 if (len != s) 181 throw new EOFException ("truncated file"); 182 return new String (data); 183 } 184 185 191 public final static String copyGZipFileToString (java.io.File f) throws java.io.IOException { 192 GZIPInputStream is = new GZIPInputStream (new FileInputStream (f)); 193 byte[] arr = new byte[256 * 256]; 194 int first = 0; 195 for(;;) { 196 int len = is.read(arr, first, arr.length - first); 197 if (first + len < arr.length) { 198 return new String (arr, 0, first + len); 199 } 200 } 201 } 202 203 210 public final static File copyStringToFile (File f, String content) throws Exception { 211 FileOutputStream os = new FileOutputStream (f); 212 InputStream is = new ByteArrayInputStream (content.getBytes("UTF-8")); 213 FileUtil.copy(is, os); 214 os.close (); 215 216 return f; 217 } 218 219 private final static File copyStringToFile(String filename, String res) throws Exception { 220 File f = new File (tempDirectory, filename); 221 f.deleteOnExit (); 222 return copyStringToFile(f, res); 223 } 224 225 private final static File copyResourceToFile(URL u) throws Exception { 226 File f = File.createTempFile("res", ".xml"); 227 f.deleteOnExit (); 228 229 FileOutputStream os = new FileOutputStream (f); 230 InputStream is = u.openStream(); 231 FileUtil.copy(is, os); 232 os.close (); 233 234 return f; 235 } 236 237 private static File tempDirectory; 238 { 239 try { 240 File f = File.createTempFile("foo", "bar"); 241 tempDirectory = f.getParentFile(); 242 } catch (IOException e) { 243 tempDirectory = new File ("/tmp"); 244 } 245 } 246 } | Popular Tags |