import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.novell.java.io.DataAccessable;
import com.novell.java.io.NOutputStream;
import com.novell.java.io.NInputStream;
import com.novell.java.io.RandomAccess;
import com.novell.utility.naming.Environment;
class Streams
{
public static void main(String args[])
{
if (args.length < 1)
{
help();
}
String url = args[0];
StaticAttributeValueInterface sai = new TextualStatic(true);
Hashtable systemProps = new Hashtable();
systemProps.put(
Context.INITIAL_CONTEXT_FACTORY,
Environment.FS_INITIAL_CONTEXT_FACTORY);
systemProps.put(Context.PROVIDER_URL, url);
boolean osOpen = false;
boolean isOpen = false;
boolean raOpen = false;
OutputStream os = null;
InputStream is = null;
RandomAccess ra = null;
try
{
Object obj = new InitialContext(systemProps).lookup("");
System.out.println("\ncheckin data access on" + url + "\n");
if (obj instanceof DataAccessable)
{
is = new NInputStream((DataAccessable)obj);
isOpen = true;
byte[] buffer = new byte[80];
int count;
do
{
count = is.read(buffer);
System.out.println(new String(buffer));
}
while(count != -1);
is.close();
isOpen = false;
os = new NOutputStream((DataAccessable)obj);
osOpen = true;
sai.writeStream(os);
os.close();
osOpen = false;
ra = new RandomAccess((DataAccessable)obj);
raOpen = true;
sai.readRandom(ra);
ra.close();
raOpen = false;
}
else
{
System.out.println("error: " + url + " is not DataAccessable");
System.exit(-1);
}
}
catch (IOException ioe)
{
try
{
if (osOpen)
os.close();
if (isOpen)
is.close();
if (raOpen)
ra.close();
}
catch (IOException nested)
{
System.out.println("error with close in catch: " + nested);
nested.printStackTrace();
}
System.out.println("error with stream: " + ioe);
ioe.printStackTrace();
System.exit(-1);
}
catch(javax.naming.NamingException e)
{
System.out.println("\nException thrown: " + e);
e.printStackTrace();
System.exit(-1);
}
System.exit(0);
}
private static void help()
{
System.out.println(
"\nTo use this example program enter the following:\n");
System.out.println("\tjava Streams <url>\n");
System.out.println(
"\t\turl = name of an existing file to do streams testing on");
System.out.println("\t\t my_server/my_volume/my_dir/my_file");
System.out.println("");
System.out.println("Warning: file data will be lost in specified file!");
System.out.println("");
System.exit(-1);
}
}