536 |
Resource getResource(String name) |
Resource getResource(String name) |
537 |
{ |
{ |
538 |
try |
try |
539 |
{ |
{ |
540 |
File file = new File(dir, name).getCanonicalFile(); |
File file = new File(dir, name).getCanonicalFile(); |
541 |
if (file.exists() && !file.isDirectory()) |
if (file.exists() && !file.isDirectory()) |
542 |
return new FileResource(this, file); |
return new FileResource(this, file); |
543 |
} |
} |
544 |
catch (IOException e) |
catch (IOException e) |
545 |
{ |
{ |
546 |
// Fall through... |
// Fall through... |
547 |
} |
} |
548 |
return null; |
return null; |
549 |
} |
} |
550 |
} |
} |
873 |
// construct the class (and watch out for those nasty IOExceptions) |
// construct the class (and watch out for those nasty IOExceptions) |
874 |
try |
try |
875 |
{ |
{ |
876 |
byte[] data; |
byte[] data; |
877 |
InputStream in = resource.getInputStream(); |
InputStream in = resource.getInputStream(); |
878 |
try |
try |
879 |
{ |
{ |
880 |
int length = resource.getLength(); |
int length = resource.getLength(); |
881 |
if (length != -1) |
if (length != -1) |
882 |
{ |
{ |
883 |
// We know the length of the data. |
// We know the length of the data. |
884 |
// Just try to read it in all at once |
// Just try to read it in all at once |
885 |
data = new byte[length]; |
data = new byte[length]; |
886 |
int pos = 0; |
int pos = 0; |
887 |
while (length - pos > 0) |
while (length - pos > 0) |
888 |
{ |
{ |
889 |
int len = in.read(data, pos, length - pos); |
int len = in.read(data, pos, length - pos); |
890 |
if (len == -1) |
if (len == -1) |
891 |
throw new EOFException("Not enough data reading from: " |
throw new EOFException("Not enough data reading from: " |
892 |
+ in); |
+ in); |
893 |
pos += len; |
pos += len; |
894 |
} |
} |
895 |
} |
} |
896 |
else |
else |
897 |
{ |
{ |
898 |
// We don't know the data length. |
// We don't know the data length. |
899 |
// Have to read it in chunks. |
// Have to read it in chunks. |
900 |
ByteArrayOutputStream out = new ByteArrayOutputStream(4096); |
ByteArrayOutputStream out = new ByteArrayOutputStream(4096); |
901 |
byte[] b = new byte[4096]; |
byte[] b = new byte[4096]; |
902 |
int l = 0; |
int l = 0; |
903 |
while (l != -1) |
while (l != -1) |
904 |
{ |
{ |
905 |
l = in.read(b); |
l = in.read(b); |
906 |
if (l != -1) |
if (l != -1) |
907 |
out.write(b, 0, l); |
out.write(b, 0, l); |
908 |
} |
} |
909 |
data = out.toByteArray(); |
data = out.toByteArray(); |
910 |
} |
} |
911 |
} |
} |
912 |
finally |
finally |
913 |
{ |
{ |
914 |
in.close(); |
in.close(); |
915 |
} |
} |
916 |
final byte[] classData = data; |
final byte[] classData = data; |
917 |
|
|
918 |
// Now get the CodeSource |
// Now get the CodeSource |
919 |
final CodeSource source = resource.getCodeSource(); |
final CodeSource source = resource.getCodeSource(); |