• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

conversion from inputstream to fileinputstream

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final String classLocation = clsname.replace('.', '/') + ".class";
cs = new DataInputStream( ClassLoader.getSystemResource(classLocation).openStream());


"ClassLoader.getSystemResource(classLocation).openStream()" this is returning inputstream but i want that to be FileInputstream , and i tried to type cast with fileinputstream but it returning null value.

is there any solution with out type casting..


Thanks in Advance..
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> but it returning null value
the resource is *not* there on the classpath.
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to read from jar file

System.out.println(ClassLoader.getSystemResource(classLocation).getPath());

this statement prints

file:/C:/Program%20Files/Java/j2re1.4.2_06/lib/rt.jar!/java/util/Date.class

and if i dont type caste its working fine , null is coming only if i type caste .
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> and if i dont type caste its working fine , null is coming only if i type caste
are you sure? It *must* be throwing ClassCastException
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> yes it is throwing "ClassCastException" . how to make it
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As i said earlier the getResourceAsStream function returns InputStream as the url might point to different location, could be an
entry in the jar file (thats what happening in your case) - in this case there is no file, So it returns a ByteArrayInputStream
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With other words, as the stream is not (directly) reading from a file, it cannot possibly be a FileInputStream.

But why would you want to have one, anyway? What are you trying to accomplish?
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to read class file from jar , if it is FileInputStream i could read even read tags like Deprecated , when coming to InputStream i am able to read only class but not tags , but my purpose is to read even tags
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for that java has something called "Reflection"
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prasad Venkat:
if it is FileInputStream i could read even read tags like Deprecated



How do you do that??? After all, the stream content is the same...
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reflection cant provide info about "deprecated"..

"How do you do that??? After all, the stream content is the same..."


when i extracted a class file to a location , and tried to read that through fileinputstream i could read tag like deprecated .

when i tried to read from jar directly through inputstream it is not reading that deprecated tag info
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do my problem have solution
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not following you yet. Could you show the working code with FileInputStream?
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this works for me (uses reflection)

 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I AM WORKING ON JAVA 1.4 WHERE ANNOTATION WILL NOT SUPPORT WHICH SUPPORTS FROM JAVA 5


This code able read even info about Deprecated Tag which is kept like shown below in a class file.
/**
* @deprecated
*
*/

cs = new DataInputStream(new FileInputStream(fileName)); // filename give the complete path of class file(Date.class extracted manually and kept in current folder and tested)// passing FileInputStream obj to DataInputstream constructor


cs.readInt(); // holds info about magic number
cs.readUnsignedShort();// holds info about minor version
cs.readUnsignedShort();// holds info about major version
.....
....
....
After reading Date.class o/p is

UTF: Date
2. Class: 1
3. UTF: java/lang/Object
4. Class: 3
5. UTF: java/io/Serializable
6. Class: 5
7. UTF: java/lang/Cloneable
8. Class: 7
9. UTF: java/lang/Comparable
10. Class: 9
11. UTF: fastTime
12. UTF: J
13. UTF: defaultCenturyStart
14. UTF: I
15. UTF: serialVersionUID
16. UTF: ConstantValue
19. UTF: simpleFormatter
20. UTF: Ljava/lang/ref/SoftReference;
21. UTF: gmtFormatter
22. UTF: wtb
23. UTF: [Ljava/lang/String;
24. UTF: ttb
25. UTF: [I
26. UTF: getDate
27. UTF: ()I
28. UTF: Deprecated <------------- which shows here
29. UTF: Code
30. UTF: java/lang/Error
31. Class: 30

------------------------------

This code not able to read info about Deprecated Tag when reading with inputstream from jar file directly with out extracting like previous one.
same progarm and logic but o/p is diffrent

String classLocation = clsname.replace('.', '/') + ".class"; // class location holds package info eg : java.util.Date converted to java/util/Date

cs = new DataInputStream(ClassLoader.getSystemResource(classLocation).openStream()); // passing InputStream obj to DataInputstream constructor

cs.readInt(); // holds info about magic number
cs.readUnsignedShort();// holds info about minor version
cs.readUnsignedShort();// holds info about major version
.........
.........
.......

After reading Date.class (Deprecated not shown here) o/p is
1. Integer: -2147483648
2. Integer: 60000
3. String: 42
4. String: 44
5. String: 48
6. String: 49
7. String: 50
8. String: 53
9. String: 58
10. String: 60
11. String: 61
12. String: 63
13. String: 65
14. String: 67
15. String: 69
16. String: 86
17. String: 89
18. String: 110
19. String: 111
20. String: 114
21. String: 115
22. String: 116
23. String: 117
24. String: 118
25. String: 119
26. String: 120
27. String: 122
28. String: 123
29. String: 124
30. String: 128
31. String: 129
32. String: 144
33. String: 145
34. String: 150
35. String: 151
36. String: 152
37. String: 154
38. UTF: <clinit>
39. UTF: <init>
40. UTF: Code
41. UTF: ConstantValue
42. UTF: EEE MMM dd HH:mm:ss zzz yyyy
43. UTF: Exceptions
44. UTF: GMT
45. UTF: US
46. UTF: UTC
47. UTF: after
48. UTF: am
49. UTF: april
50. UTF: august
51. UTF: before
52. UTF: cal
53. UTF: cdt
54. UTF: charAt
55. UTF: clear
56. UTF: clone
57. UTF: compareTo
58. UTF: cst
59. UTF: currentTimeMillis
60. UTF: d MMM yyyy HH:mm:ss 'GMT'
61. UTF: december
62. UTF: defaultCenturyStart
63. UTF: edt
64. UTF: equals
65. UTF: est
66. UTF: fastTime
67. UTF: february
68. UTF: format
69. UTF: friday
70. UTF: get
71. UTF: getDate
72. UTF: getDateTimeInstance
73. UTF: getDay
74. UTF: getDefault
75. UTF: getField
76. UTF: getHours
77. UTF: getMinutes
78. UTF: getMonth

[ January 18, 2007: Message edited by: prasad Venkat ]

[ January 18, 2007: Message edited by: prasad Venkat ]
[ January 18, 2007: Message edited by: prasad Venkat ]
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it should not matter whether you are using FileInputStream or any other InputStream for DataInputStream constructor
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then i dont understand what made the o/p different..
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post the code that is reading the class file
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.text.Annotation;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;




class ClassReader {


public ClassReader(String crpath, String string) {

fileName = crpath; // stores info of file name
clsname=string;
}

public ClassReader(String string) {

clsname=string;
fileName="empty";
}

/**
* This method identifies version and other info like methods and feilds of class
* @return Map
*/

public Map versionInfo() {


boolean flag=false;
String urlString=null;
System.out.println("Ur Using JAVA "
+ System.getProperty("java.version"));

try {
if(fileName.equals("empty")){
flag = true;
}
if(!flag ){
cs = new DataInputStream(new FileInputStream(fileName));
}
}catch(IOException e){
flag=true;
}
try{
if(flag){


final String classLocation = clsname.replace('.', '/') + ".class";
cs = new DataInputStream(ClassLoader.getSystemResource(classLocation).openStream());

}
cs.readInt(); // holds info about magic number of class file format
cs.readUnsignedShort();// holds info about minor version of class file format
cs.readUnsignedShort();// holds info about major version of class file format

// Read constant pool
int constantCount = cs.readUnsignedShort();
UTStrings = new String[constantCount];
for (int i = 1; i < constantCount; i++) {
if(i==305){
System.out.println("sddfs");
}
if(readsConstantPool(i)){
++i;
}
}

// Read basic class information
cs.readShort(); // Reading access flags
cs.readShort(); // Reading this class
cs.readShort(); // Reading superclass

//This for reading interfaces
int interfaceCount = cs.readShort();
System.out.println("interfaceCount" + interfaceCount);
readInterfaceInfo(interfaceCount);

// Read fields
int fieldCount = cs.readUnsignedShort();
for (int i = 0; i < fieldCount; i++)
readMethodsOrFieldsInfo("Field");

// Read methods
int methodCount = cs.readUnsignedShort();
for (int i = 0; i < methodCount; i++)
readMethodsOrFieldsInfo("Method");

// Read top-level attributes (of entire class)
int attributeCount = cs.readUnsignedShort();

}catch(ClassCastException ce){
System.out.println (ce.getMessage());
}
catch (Exception e) {
System.out.println (e.getMessage());
}


/*for (int m = 1; m < UTStrings.length; m++) {

if (UTStrings[m] == null)
continue;
if ((UTStrings[m] == thisClass) ||
(UTStrings[m] == superClass))
continue;
if (UTStrings[m].type == UTStrings.CLASS) {
String s = UTStrings[i].arg1.strValue;
if (s.charAt(0) == '[')
continue;
s = printClassName(UTStrings[i].arg1.strValue);
if ((packageName != null) && (s.startsWith(packageName)))
continue;
System.out.println(printClassName(s)+";");
}
}*/
return map;
}

/**
* read constant number 'k' in the constant pool
* @param k
* @throws IOException
*/

private boolean readsConstantPool(int k) throws IOException {

byte tag = cs.readByte();
boolean ldflag=false;

if (tag == 1) { // UTF text
String stg = cs.readUTF();
System.out.println(k + ". UTF: " + stg);
UTStrings[k] = stg;
//map.put(stg,"CP");
cpcnt++;

} else if (tag == 2) {// unused tag

} else if (tag == 3) {
int intValue = cs.readInt();
System.out.println(k + ". Integer: " + intValue);

} else if (tag == 4) {// float
cs.readFloat();

} else if (tag == 5) {// long
cs.readLong();
ldflag=true;

} else if (tag == 6) {// double
cs.readDouble();
ldflag=true;

} else if (tag == 7) {// class
int UTFindex1 = cs.readUnsignedShort();
System.out.println(k + ". Class: " + UTFindex1);
//map.put(UTStrings[UTFindex1],"Pack");

} else if (tag == 8) {// String
int UTFindex2 = cs.readUnsignedShort();
System.out.println(k + ". String: " + UTFindex2);

} else if (tag == 9) {// Fieldref
int class1 = cs.readUnsignedShort();
int nameAndType1 = cs.readUnsignedShort();
System.out
.println(k + ". Fieldref: " + class1 + "/" + nameAndType1);

} else if (tag == 10) {// Methodref
int class2 = cs.readUnsignedShort();
int nameAndType2 = cs.readUnsignedShort();
System.out
.println(k + ". Fieldref: " + class2 + "/" + nameAndType2);

} else if (tag == 11) {// Interface Methodref
cs.readUnsignedShort();
cs.readUnsignedShort();

} else if (tag == 12) {// NameAndType
int name = cs.readUnsignedShort();
int type = cs.readUnsignedShort();
System.out.println(k + ". NameAndType: " + name + " " + type);
}
return ldflag;
}


private static void readInterfaceInfo(int count) throws IOException {

System.out.print("count:" + count);

for (int i = 0; i < count; ++i) {
short tag = cs.readShort();
if (tag > 1 && tag < cpcnt-1){
System.out.print(UTStrings[tag]);
}
}
}


/**
* read a field or method
* @param label
* @throws IOException
*/

private void readMethodsOrFieldsInfo(String label) throws IOException {

cs.readUnsignedShort();
int name = cs.readUnsignedShort();
int type =cs.readUnsignedShort();

System.out.println ();
System.out.println (label + " name: " + name +
" [" + UTStrings[name] + "]");
System.out.println ("Type: " + type +
" [" + UTStrings[type] + "]");
int attributeCount = cs.readUnsignedShort();
//if (attributeCount > 1 && attributeCount < cpcnt - 1){
for (int i = 0; i < attributeCount; i++) {

int attributeName = cs.readUnsignedShort();
//if (attributeName > 1 && attributeName < cpcnt - 1){
try{
if (UTStrings[attributeName].equals("Deprecated")) {
map.put(UTStrings[name], "Deprecated");
System.out.println(name + "depreacted");
}
int length = cs.readInt();
readAttributeValue(length);
}catch(Exception e){}
//}
}
//}

}

/**
* read the value of an attribute: a sequence of 'length' bytes
* @param length
* @throws IOException
*/

public void readAttributeValue(int length) throws IOException {
for (int i = 0; i < length; i++) {
int val = cs.readUnsignedByte() + (0x100);

}


}

public void readClassName(String nam) {
fileName = nam;

}

/* private String printClassName(String s) {
StringBuffer x;

if (s.charAt(0) == '[') {
return(typeString(s, ""));
}
x = new StringBuffer();
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == '/')
x.append('.');
else
x.append(s.charAt(j));
}
return (x.toString());

}*/

static DataInputStream cs;

static String[] UTStrings = null;

static String fileName = null;

static int cpcnt=0;

private String clsname=null;

Map map = new HashMap();
}
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just curious why the fields are static here
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and for me its printing the deprecated method flag as well

Method name: 61 [<init>]
Type: 51 [(III)V]
61depreacted
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think you have tried with class written by you , and that in the current folder


try with existing package in some jar e.g : java.util.Date
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no i tried with java.util.Date
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why i am not getting , i am using java 1.4.2, on what version you are working
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is showing all deprecated info on java 1.5 but not java 1.4 , what will be the solution for 1.4
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is There any Jar Specification diff in java1.4 and Java 5
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic