Solved

Java 1.4 NIO

Posted on 2002-07-08
7
377 Views
Last Modified: 2013-12-29
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
Comment
Question by:ottob
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 7140035
You'd better post reference to this question at JavaScript forum too.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 7140149
Unpack this and open retriever.html. The page will need adjusting:
http://www.sigma-software.freeserve.co.uk/misc/retriever.zip
0
 

Author Comment

by:ottob
ID: 7141962
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ottob
ID: 7141963
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7142621
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
 

Author Comment

by:ottob
ID: 7144900
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
 

Author Comment

by:ottob
ID: 7151602
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web application structure 18 127
SHA2 certs for IIS AND Java? 2 134
mysql jsp example issue 32 92
convert Absolute path to relative path of FTP location 7 47
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question