UC Berkeley Group for User Interface Research
Updated November 17, 2000

edu.berkeley.guir.lib.net
Class NetLib

java.lang.Object
  |
  +--edu.berkeley.guir.lib.net.NetLib

public class NetLib
extends Object

Miscellaneous utilities for using networking. Includes:

This software is distributed under the Berkeley Software License.

 Revisions:  - GUIRLib-v1.2-1.0.0, Jul 16 2000, JH
               Created class
             - GUIRLib-v1.3-1.0.0, Aug 11 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.4-1.0.0, Aug 31 2000, JH
               Touched for GUIRLib release
 

Since:
JDK 1.1.6
Version:
GUIRLib-v1.4-1.0.0, Aug 31 2000
Author:
Jason Hong ( jasonh@cs.berkeley.edu )

Constructor Summary
NetLib()
           
 
Method Summary
static int bytesToLen(byte[] bSize)
          Convert the first four bytes in the byte array into an int.
static URL FileNameToURL(String strFileName)
          Given a filename, convert it to a URL.
static String getBottomLevelDomainName(String strAddress)
          Given an address like "www.cs.berkeley.edu", return "www".
static String getLocalBottomLevelDomainName()
          Return the first name of the local machine.
static byte[] lenToBytes(byte[] bPayload)
          Take the length of the payload, and convert to bytes, returning the bytes in a newly created array of size 4.
static byte[] lenToBytes(byte[] bPayload, byte[] bSize)
          Take the length of the payload, and convert to bytes, returning the bytes in the byte array passed in.
static byte[] lenToBytes(int len)
          Take an int, chops the int into bytes containing the bits, returning the bytes in a newly created byte-array.
static byte[] lenToBytes(int len, byte[] bSize)
          Take an int, chops the int into bytes containing the bits, returning the bytes in the byte array passed in.
static void main(String[] argv)
           
static byte[] read(InputStream istream)
          Synchronously read using our protocol.
static String URLToFileName(String str)
          Given a URL, convert it to a filename.
static String URLToFileName(URL url)
          Given a URL, convert it to a filename.
static void write(OutputStream ostream, byte[] bPayload)
          Synchronously write out using our protocol.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetLib

public NetLib()
Method Detail

lenToBytes

public static byte[] lenToBytes(byte[] bPayload)
Take the length of the payload, and convert to bytes, returning the bytes in a newly created array of size 4.
Parameters:
bPayload - is the payload. Automatically takes length of bPayload to be the full size of the payload.
Returns:
byte array size 4 containing the length.

lenToBytes

public static byte[] lenToBytes(byte[] bPayload,
                                byte[] bSize)
Take the length of the payload, and convert to bytes, returning the bytes in the byte array passed in.
Parameters:
bPayload - is the payload. Automatically takes length of bPayload to be the full size of the payload.
bSize - is the byte array to put the length value in. Must be at least length 4. Puts the length value into the first four indices, 0-3.
Returns:
a reference to bSize.

lenToBytes

public static byte[] lenToBytes(int len)
Take an int, chops the int into bytes containing the bits, returning the bytes in a newly created byte-array.
Parameters:
len - is the value to convert to bytes.
Returns:
byte array size 4 containing the length.

lenToBytes

public static byte[] lenToBytes(int len,
                                byte[] bSize)
Take an int, chops the int into bytes containing the bits, returning the bytes in the byte array passed in.
Parameters:
len - is the value to convert to bytes.
bSize - is the byte array to put the length value in. Must be at least length 4. Puts the length value into the first four indices, 0-3.
Returns:
a reference to bSize.

bytesToLen

public static int bytesToLen(byte[] bSize)
Convert the first four bytes in the byte array into an int.

write

public static void write(OutputStream ostream,
                         byte[] bPayload)
                  throws IOException
Synchronously write out using our protocol.

Again, the protocol is first four bytes for length of payload, rest of bytes is the payload.

Parameters:
ostream - is the OutputStream to write out to.
bPayload - is the payload of bytes to write out.

read

public static byte[] read(InputStream istream)
                   throws IOException
Synchronously read using our protocol.

Again, the protocol is first four bytes for length of payload, rest of bytes is the payload.

Parameters:
istream - is the InputStream to read in from.
Returns:
an array of bytes, or null if nothing to read.

getBottomLevelDomainName

public static String getBottomLevelDomainName(String strAddress)
Given an address like "www.cs.berkeley.edu", return "www".
Parameters:
strAddress - is the name of the machine.
Returns:
the first part of the address.

getLocalBottomLevelDomainName

public static String getLocalBottomLevelDomainName()
Return the first name of the local machine. For example, if you were on "exuma.cs.berkeley.edu", then return "exuma".
Returns:
the first part of the address of the local machine.

URLToFileName

public static String URLToFileName(URL url)
Given a URL, convert it to a filename.
Parameters:
url - contains a file protocol url.
Returns:
the file portion, converting from '|' to ':' if necessary. Returns null if not a file protocol url.

URLToFileName

public static String URLToFileName(String str)
Given a URL, convert it to a filename.
Parameters:
str - is a String containing a URL.
Returns:
the file portion, converting from '|' to ':' if necessary. Returns null if not a file protocol url.

FileNameToURL

public static URL FileNameToURL(String strFileName)
Given a filename, convert it to a URL.
Returns:
a URL object, or null if there is an error converting to a URL.

main

public static void main(String[] argv)
                 throws Exception

Copyright Information