1 23 24 package org.objectweb.clif.util.bytearray; 25 26 import org.objectweb.clif.util.ClifClassLoader; 27 import java.net.URLConnection ; 28 import java.net.URL ; 29 import java.io.IOException ; 30 import java.io.ByteArrayInputStream ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 34 35 39 public class ByteArrayURLConnection extends URLConnection 40 { 41 String name; 42 ClifClassLoader loader; 43 byte[] bytes; 44 45 46 public ByteArrayURLConnection(URL url, ClifClassLoader classloader) 47 { 48 super(url); 49 name = url.getFile(); 50 loader = classloader; 51 connected = false; 52 } 53 54 55 public void connect() 56 { 57 if (! connected) 58 { 59 try 60 { 61 bytes = loader.getBytes(name); 62 connected = true; 63 } 64 catch (IOException ex) 65 { 66 67 } 68 } 69 } 70 71 72 public int getContentLength() 73 { 74 if (! connected) 75 { 76 connect(); 77 } 78 return bytes.length; 79 } 80 81 82 public String getContentType() 83 { 84 String type = guessContentTypeFromName(name); 85 if (type == null) 86 { 87 if (! connected) 88 { 89 connect(); 90 } 91 try 92 { 93 type = guessContentTypeFromStream(new ByteArrayInputStream (bytes)); 94 } 95 catch (IOException ex) 96 { 97 } 98 } 99 return type; 100 } 101 102 103 public Object getContent() 104 throws IOException 105 { 106 if (! connected) 107 { 108 connect(); 109 } 110 return bytes; 111 } 112 113 114 public InputStream getInputStream() 115 throws IOException 116 { 117 if (! connected) 118 { 119 connect(); 120 } 121 return new ByteArrayInputStream (bytes); 122 } 123 124 125 public OutputStream getOutputStream() 126 throws IOException 127 { 128 throw new IOException ("bytearray URLs cannot be written to"); 129 } 130 131 132 public String toString() 133 { 134 return "ByteArrayURLConnection for " + loader + ", URL " + getURL(); 135 } 136 } 137 138 | Popular Tags |