• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

Java 1.4 NIO

This Is A Pretty Complex Chalenge And I Expect To Use 1500 Points Or More To Get Results
I Have A Small Applet That Kind Of Does What I What This To Do But I Don't Lost The Code Sometime Ago.
Also I Don't Know Java Well Enough To Even Compile It Myself. Thats The Main Reason I'm Offering So Many Points.
The Applet Need To Be Writen Compiled And Sent To Me For An Excepted Answer
With That Behind Me, Heres What I Do Have:

The HTML Reads:
<----------------------------------->
<html><head>
<script>
res="";

function on_load(){
     document.announcer_java.start_announcing();
     check_stuff_from_applet();
}

function check_stuff_from_applet(){
res = document.announcer_java.get_achat();
setTimeout("rewrite();", 500);
}

function rewrite(){
self.parent.frames[1].document.close();
self.parent.frames[1].document.open();
self.parent.frames[1].document.write("<HTML><BODY bgcolor='#ffffbb'>");
self.parent.frames[1].document.write( res );
self.parent.frames[1].document.write("</body></html>")
setTimeout("check_stuff_from_applet();", 500);
}

</script>
</head>
<BODY BGCOLOR="#aaaaaa" text="000000" onLoad="on_load();">
<applet code=announcer.class name="announcer_java" MAYSCRIPT width=2 height=2>
<PARAM NAME=Chat_Location VALUE="/risk/risk.txt">
<PARAM NAME=List_Location VALUE="/chat/list.txt">
<PARAM NAME=Script_Location VALUE="cgi-bin/chat.pl?aname="otto">
<PARAM NAME=delay VALUE=500></applet>
</BODY></html>
<--------------------------------------------------->
And Can Be Found With The Applet At:
how-2.com/risk/index.htm
how-2.com/risk/applet.htm
how-2.com/risk/announcer.class
how-2.com/risk/risk.txt

Although I'm Not Using The list.txt And chat.pl Yet.

I Would Rather Run Everything From JS, So Here's I What I'm Looking For

Instead Of :
document.announcer_java.start_announcing();
Starting The Loop Delay Inside The Applet
I'd Rather Just Tell The Applet, (With A Call To A Function From JS), To Read The Text File Into A NIO Buffer (as if i know what i'm talking about)
And Then Have That Function Return The Text File As A Variable To The JS Funtion That Called It.
This Way I Can Have JS Loop Instead.
Furthermore I Want To Be Able To Pass The Peramiters Of This Applet To It In The Funtion Call So I Can Access Text From Different Files.

This Would Be What The HTML Might Look Like:
<----------------------------------->
<html><head>
<script>
texta="";
textb="";
function on_load(){
     check_stuff_using_applet();
}

function check_stuff_using_applet(){
texta = document.retriever_java.get_text_file("filea.txt");
textb = document.retriever_java.get_text_file("fileb.txt");
setTimeout("rewrite();", 500);
}

function rewrite(){
self.parent.frames[1].document.close();
self.parent.frames[1].document.open();
self.parent.frames[1].document.write("<HTML><BODY bgcolor='#ffffbb'>");
self.parent.frames[1].document.write( texta );
self.parent.frames[1].document.write("</body></html>")
self.parent.frames[2].document.close();
self.parent.frames[2].document.open();
self.parent.frames[2].document.write("<HTML><BODY bgcolor='#ffffbb'>");
self.parent.frames[2].document.write( textb );
self.parent.frames[2].document.write("</body></html>")
setTimeout("check_stuff_from_applet();", 500);
}

</script>
</head>
<BODY BGCOLOR="#aaaaaa" text="000000" onLoad="on_load();">
<applet code=retriever.class name="retriever_java" MAYSCRIPT width=2 height=2>
</BODY></html>
<--------------------------------------------------->
0
ottob
Asked:
ottob
  • 4
  • 2
1 Solution
 
heyhey_Commented:
You'd better post reference to this question at JavaScript forum too.
0
 
CEHJCommented:
Unpack this and open retriever.html. The page will need adjusting:
http://www.sigma-software.freeserve.co.uk/misc/retriever.zip
0
 
ottobAuthor Commented:
CEHJ,
First Off, Thats Works Great,
But Is This Applet Written And Compiled In JDK 1.4?
And I Did Not Notice The import java.nio.*
Nor A .getchannel().
Reguardless Its Sweet.
Please Let Me Know If I Can Post The Code Or If You Don't Want The Code Posted For Future Previous Asked Question Patrons.
I'm Sending These 500 And Another 500 For Now.
But I Have At Least Another 1500 More Than That For You 500 At A Time.
Just Say When.
THANKS
Ottob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ottobAuthor Commented:
CEHJ,
First Off, Thats Works Great,
But Is This Applet Written And Compiled In JDK 1.4?
And I Did Not Notice The import java.nio.*
Nor A .getchannel().
Reguardless Its Sweet.
Please Let Me Know If I Can Post The Code Or If You Don't Want The Code Posted For Future Previous Asked Question Patrons.
I'm Sending These 500 And Another 500 For Now.
But I Have At Least Another 1500 More Than That For You 500 At A Time.
Just Say When.
THANKS
Ottob
0
 
CEHJCommented:
Please feel free to post the code, although, having done it in a hurry for you, it is inadequately commented.

The final compilation was not done in JDK 1.4.

I'm not clear why you want to use classes that are specific to JDK 1.4. They are not supported in the runtimes of browsers. I think it might be possible to parcel up the new classes and include them in a downloadable archive.

Why the java.nio.*?
0
 
ottobAuthor Commented:
I THought It Was Goint To Be Faster
Although This Is So Small Speed Will No Be An Issue
Access Time Will Be The Issue
But I Guess Not
No Problem
I Will Post The Code Later.
Thanks Again
Otto
0
 
ottobAuthor Commented:
The Code

retriever.html
<----------------------------->
<html>
<head>
<script language="JavaScript">
texta="";
textb="";
function on_load(){

check_stuff_using_applet();
}
function check_stuff_using_applet(){
alert(document.applets[0].getAppletInfo());
texta = document.applets[0].get_text_file("filea.txt");
alert(texta);
}

/*
function rewrite(){
self.parent.frames[1].document.close();
self.parent.frames[1].document.open();
self.parent.frames[1].document.write("<HTML><BODY bgcolor='#ffffbb'>");
self.parent.frames[1].document.write( texta );
self.parent.frames[1].document.write("</body></html>")
self.parent.frames[2].document.close();
self.parent.frames[2].document.open();
self.parent.frames[2].document.write("<HTML><BODY bgcolor='#ffffbb'>");
self.parent.frames[2].document.write( textb );
self.parent.frames[2].document.write("</body></html>")
setTimeout("check_stuff_from_applet();", 500);

}
*/
</script>
</head>
<BODY BGCOLOR="#aaaaaa" text="000000" onLoad="on_load();">
  <applet codebase="." code="retriever.class" name="retriever_java" MAYSCRIPT width=2 height=2></applet>
</BODY></html>

<-------------------------------------------------------->



retriever.java
<-------------------------------------------------------->
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.Applet;

/**
 *Description of the Class
 *
 * @author     protean
 * @created    09 July 2002
 */
public class retriever extends Applet {
      /**
       *Description of the Method
       *
       * @param  fileName  Description of Parameter
       * @return           Description of the Returned Value
       */
      public String get_text_file(String fileName) {
            try {
                  System.out.println("Applet called!");
                  URL url = new URL(getCodeBase(), fileName);
                  URLReader reader = new URLReader(url);
                  reader.setReadAll(true);
                  return reader.read().toString();
            } catch (Exception e) {
                  if (e instanceof FileNotFoundException) {
                        System.err.println("Could not find file " + fileName);
                  }
                  e.printStackTrace();
            }
            return null;
      }


      /**
       *Gets the appletInfo attribute of the retriever object
       *
       * @return    The appletInfo value
       */
      public String getAppletInfo() {
            return "Title: Text File Retriever \n"
                         + "Author: CEHJ";
      }
}

/**
 *Description of the Class
 *
 * @author     protean
 * @created    09 July 2002
 */
class URLReader {


      /**
       *Description of the Field
       */
      protected Reader in = null;
      /**
       *Description of the Field
       */
      protected final int defBufSize = 1024;
      /**
       *Description of the Field
       */
      protected boolean unprettyPrint = false;
      URLConnection conn;
      URL url;
      int bufSize;
      char[] buf;
      char[] blankBuf;
      boolean readAll = true;
      StringBuffer stringBuffer;


      /**
       *Constructor for the URLReader object
       *
       * @param  strURL  Description of Parameter
       */
      public URLReader(String strURL) {
            try {
                  bufSize = defBufSize;
                  url = new URL(strURL);
            } catch (MalformedURLException eURL) {
                  System.err.println("\nThe URL specified to URLReader() is invalid");
            }
            try {
                  conn = url.openConnection();
                  conn.connect();
            } catch (IOException eIO) {
                  System.err.println("Failed to connect to URL specified in URLReader(): [" + strURL + "] - Are you connected to the network\\internet?");
            }
      }


      /**
       *Constructor for the URLReader object
       *
       * @param  url  Description of Parameter
       */
      public URLReader(URL url) {
            this(url.toString());
      }


      /**
       *Sets the unprettyPrint attribute of the URLReader object
       *
       * @param  value  The new unprettyPrint value
       */
      public void setUnprettyPrint(boolean value) {
            unprettyPrint = value;
      }


      /**
       *Sets the readAll attribute of the URLReader object
       *
       * @param  readAll  The new readAll value
       */
      public void setReadAll(boolean readAll) {
            this.readAll = readAll;
      }


      /**
       *Sets the bufSize attribute of the URLReader object
       *
       * @param  bufSize  The new bufSize value
       */
      public void setBufSize(int bufSize) {
            this.bufSize = bufSize;
      }


      /**
       *Gets the reader attribute of the URLReader object
       *
       * @return                  The reader value
       * @exception  IOException  Description of Exception
       */
      public Reader getReader() throws IOException {
            return (in = new BufferedReader(new InputStreamReader(conn.getInputStream())));
      }


      /**
       * We determine the amount of the url to read by calling setReadAll().
       * If setReadAll(true) called then all the file is read in defaultBufSize
       * chunks, or bufSize if the buffer size has been set with a call to setBufferSize()
       * If setReadAll(false) is called, only bufSize bytes of the url are read;
       *
       * @return    The unprettyPrint value
       */
      public boolean getUnprettyPrint() {
            return unprettyPrint;
      }


      /**
       *Gets the readAll attribute of the URLReader object
       *
       * @return    The readAll value
       */
      public boolean getReadAll() {
            return readAll;
      }


      /**
       *Gets the bufSize attribute of the URLReader object
       *
       * @return    The bufSize value
       */
      public int getBufSize() {
            return bufSize;
      }


      /**
       *Description of the Method
       *
       * @return                  Description of the Returned Value
       * @exception  IOException  Description of Exception
       */
      public StringBuffer read() throws IOException {

            stringBuffer = new StringBuffer(bufSize);
            buf = new char[bufSize];
            blankBuf = new char[bufSize];

            try {
                  if (in == null) {
                        getReader();
                  }
                  if (readAll) {
                        int c = 0;
// read the whole URL
                        while ((c = in.read()) > -1) {
                              stringBuffer.append((char) c);
                        }
                  } else {
                        in.read(buf, 0, bufSize);
                        stringBuffer.append(buf);
                  }
            } finally {
                  in.close();
            }

//return StringUtil.trimStringBuffer(stringBuffer);
            return stringBuffer;
      }
}


<---------------------------------------->

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now