ramesh gutty

Greenhorn
+ Follow
since Apr 24, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ramesh gutty

Hi Madhuri,
This is a pretty good one. I will try to answer it to the best of my knowledge, hoping that people will correct me if I am wrong. Every thread maintains a copy of variables that it works with. Now, everytime a lock is acquired/released, the JVM/Monitor ensures that it refreshes/updates the variables between the process and thread space.
I believe that while the i variable was incremented, the value of 10 must have crossed before the lock got released, now since your code doesn't check for value of i > 10 obviously it hangs(This goes in line with java spec's statement that thread policy should never be fully assumed while coding in Java. Various implementations have their own ways of doing it).
The first step to correct this would be to ensure that you check for i >10. Even after this you notice that the program doesn't exit as the JVM doesn't exit till all the threads (except daemon threads) have finished. So to make sure that your program exits, you either need to put another check in the loop of run() method or make the thread a
daemon thread.
Hey guys, correct me if I am wrong..
Regards, :-)
Ramesh gutty
Thanks Dave, Peter, Phil
Thanks very much for your support. Dave, it really seems like opening a URL Connection is the only way for a servlet to access resources from some other web server. But as Peter has pointed out, this does need some caching mechanism to make it faster. This does answer my question. Thanks a lot guys, for your help..
Cheers to JavaRanch!
Ramesh gutty
21 years ago

Hi Matt,
If you you want to only read the file, then here is a fast implementation I wrote. This class extends the
RandomAccessFile (RAF), (so that it can be casted to RAF and passed around). Internally it stores the lines
read in a collection (I have used HashMap, but you can use any collection). The readLine()
method is final in RAF, so it delegates calls to this method. This method is smart enough to not read the file,
if the line has been already ready, otherwise it reads and returns the required line.
You can modify this for file updation (basically you need to refresh the cache), if the file has very low chances
of updation
import java.io.*;
import java.util.*;
//This class extends RandomAccessFile so that it can be casted to RandomAccessFile
public class FastReader extends RandomAccessFile {
private HashMap fileContents = new HashMap(); //load factor, increment can be optimized;
//in fact we can use and collection here
private int lastLineRead = 0;
private long curPosition = 0;
public FastReader(String name, String mode) throws FileNotFoundException
{
super(name, mode);
}
public FastReader(File file, String mode) throws FileNotFoundException, IOException
{
super(file, mode);
}
//This method returns null, if eof is reached before the line number. lineNumber starts from 1, so if
//you call this method with argument with '1' you should see the first line
public String readLine(int lineNumber) throws IOException
{
if (lineNumber < 0) throw new IOException("Line number cannot be negative");<br /> //Check if we have read the linenumber till now, if so return the line directly<br /> if (lastLineRead >= lineNumber) {
return (String)fileContents.get(new Integer(lineNumber));
}
seek(curPosition);
while (lastLineRead < lineNumber) {
lastLineRead++;
fileContents.put(new Integer(lastLineRead),readLine());
}
curPosition = getFilePointer();
return (String)fileContents.get(new Integer(lastLineRead));
}
}
lemme know if this helps....
Regards,
Ramesh gutty :-)
21 years ago
Hi all,
I have a requirement of writing a java installer applet. Basically this class extends the applet and unzips a data.zip file (stored in the URL as the applet class) on the client's computer. To achieve this, I put this class in a jar file, then I created a self-signed certificate, and signed the jar file with the certificate. Now when I view the html page on Internet Explorer I get a security exception. I have seen applets, where browser brings up a prompt asking for trusting the certificate, and on clicking yes, the applet can be given full permissions. How is this possible. Can somebody help please?
Thanks,
Ramesh gutty
21 years ago
Hi gautham!
Great to know your problem was solved!! lemme know if you have any problems in future..
Regards,
Ramesh gutty
Hi Matt,
I think you can do this easily. The java.io.RandomAccessFile class provides a readLine() method. Just refer to the javadocs ;-)
Regards,
Ramesh gutty :-)
21 years ago
1. Sure you can use I/O to read gif files. In fact that's what AWT classes do. You can use a URL.openStream() method for doing this
2. This is certainly possible, but why would you want to do that.
You just need to ensure that simulate call to methods that the applet container invokes (init(), destroy() etc..), at the right time and location

Originally posted by zaeem masood ashar:
Hi
1) Can i use IO classes to read/open .gif files?
if(true){
System.out.print(how);
}

2) Can i call and dispaly an applet from within an application using io classes?

thanks


21 years ago
Hi Gautham,
Here's the code that should answer your queries. lemme know if you have any further queries on this...
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.io.File;
public class TestMutex extends Thread {
private String strThreadName;
private String lock="";
private static RandomAccessFile file;
private final static String FILENAME="winner.xml";
public TestMutex(String threadName) {
strThreadName = threadName;
}
private void init() throws Exception {
System.out.println(strThreadName+": Opening file "+FILENAME);
file = new RandomAccessFile("winner.xml", "rw");
}
public void criticalSection() throws Exception {
String strData = strThreadName + ": username\r\n" + "\r\n";
//do whatever file I/O here, this example appends the username at the end of the file
file.seek(file.length());
file.write(strData.getBytes());
}
public void run() {
synchronized (lock) {
System.out.println(strThreadName+" is working with the critical section");
try {
if (file == null) init();
criticalSection();
}
catch (Exception exp) {
System.out.println(strThreadName+"Error in criticalSection for Thread: " + strThreadName);
}
}
}
public static void main(String a[]) {
for (int i = 0; i < 5; i++) {
new TestMutex("Thread: " + i).start();
}
}
public void finalize() {
closeFile();
}
public static void closeFile() {
try {
file.close();
} catch (Exception e) {
System.out.println("Error in closing file: " +FILENAME+": "+e);
}
}
}

Regards
Ramesh Gutty :-)
Hi,
Just curious, but I needed help in this topic, Can this requirement be achieved...
The Servlet specification allows inclusion of another servlet/html page via the RequestDispatcher, but the spec clearly states that the new resource has to be within the scope of the ServletContext (page 54 of Servlet spec 2.3). Even the JSP spec mentions the same thing (Page 71 of JSP spec 1.2). Now how can we include/access resources outside the current ServletContext, e.g I have a page, that has to include contents of another site/URL in a frame, within the current page. (This can be probably achieved via browser-pull --specifying the browser to retrieve the page), but how do we do it at the server, so that the browser effectively sees it as one response from my servlet/JSP?
Would really appreciate if somebody gives me some help on this......
Thanks,
Ramesh gutty
21 years ago