org.less4j
Class Simple

java.lang.Object
  extended by org.less4j.Simple

public class Simple
extends java.lang.Object

A few utilities too simple for language experts, but damn usefull for application developers.

Copyright © 2006 Laurent A.V. Szyster

Version:
0.30

Field Summary
static char[] ALPHANUMERIC
          The strictly alphanumeric set of ASCII characters, usefull for ubiquitous identifiers on any devices and in any languages, including American English and all phone slangs.
static java.lang.String encoding
          The default 8 bit encoding for UNICODE strings
static int fioBufferSize
          The maximum block size for many file system.
static int netBufferSize
          Sixteen Kilobytes (16384 8-bit bytes) represent 3,4 pages of 66 lines and 72 columns of ASCII characters.
 
Constructor Summary
Simple()
           
 
Method Summary
static void associate(java.lang.Object[] namespace, java.util.Map object)
           
static java.lang.String decode(byte[] bytes, java.lang.String encoding)
           
static byte[] encode(java.lang.String unicode, java.lang.String encoding)
          Encode a unicode string in the given characterset encoding or use the default if a UnsupportedEncodingException was throwed.
static java.util.Iterator iterator(java.lang.Object[] objects)
          A convenience to iterate around a "primitive" array.
static java.util.Iterator itermap(java.util.Map map, java.lang.Object[] keys)
           
static java.lang.String join(java.lang.Object separator, java.util.Iterator iter)
          If you miss Python's join, here it is ;-) Synopsis ...
static java.lang.StringBuffer join(java.lang.Object separator, java.util.Iterator iter, java.lang.StringBuffer sb)
          If you miss Python's join, here it is ;-) Synopsis ...
static java.lang.String password(int length)
          Generate a random password of a given length, using only US ASCII characters.
static java.lang.String read(java.io.BufferedReader br)
           
static java.lang.String read(java.lang.String name)
          Try to read a complete file into a String.
static java.lang.String read(java.net.URL url)
           
static int recv(java.io.InputStream is, byte[] buffer, int off)
           
static java.util.Iterator split(java.lang.String splitted, char splitter)
          Returns an Iterator that splits a string with a single character as fast an lean as possible in Java (without a PCRE and for a maybe too simple use case).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALPHANUMERIC

public static char[] ALPHANUMERIC
The strictly alphanumeric set of ASCII characters, usefull for ubiquitous identifiers on any devices and in any languages, including American English and all phone slangs.


encoding

public static final java.lang.String encoding
The default 8 bit encoding for UNICODE strings

See Also:
Constant Field Values

fioBufferSize

public static int fioBufferSize
The maximum block size for many file system.


netBufferSize

public static int netBufferSize

Sixteen Kilobytes (16384 8-bit bytes) represent 3,4 pages of 66 lines and 72 columns of ASCII characters. Much more information than what most of us can digest in a few minutes. It's a reasonable maximum to set for an application web interface updated by its user every few seconds.

Also, 16KB happens to be the defacto standard buffer size for TCP peers and networks since it's the maximum UDP datagram size in use. Which makes anything smaller than this limit a better candidate for the lowest possible IP network latency.

Finally, sixteen KB buffers can hold more than 65 thousand concurrent responses in one GB of RAM, a figure between one and two orders of magnitude larger than what you can reasonably expect from a J2EE container running some commodity hardware. At an average speed of 0.5 millisecond per concurrent request/response 16KB buffers sums up to 36MBps, less than 1Gbps.

So, that sweet sixteen spot is also a safe general maximum for a web 2.0 application controller that needs to keep lean on RAM and wants the fastest possible network and the smallest possible imbalance between input and output.

Constructor Detail

Simple

public Simple()
Method Detail

associate

public static void associate(java.lang.Object[] namespace,
                             java.util.Map object)

decode

public static java.lang.String decode(byte[] bytes,
                                      java.lang.String encoding)
Parameters:
bytes -
encoding -
Returns:

encode

public static byte[] encode(java.lang.String unicode,
                            java.lang.String encoding)
Encode a unicode string in the given characterset encoding or use the default if a UnsupportedEncodingException was throwed.

Parameters:
unicode - the String to encode
encoding - the character set name
Returns:
a array of byte

iterator

public static java.util.Iterator iterator(java.lang.Object[] objects)
A convenience to iterate around a "primitive" array.

Synopsis

...

Iterator iter = Simple.iterator(new Object[]{x, y, z});
Usefull to iterate through final arrays, a prime construct in web application controllers where check lists and filter sets made of primitive array abound (usually to enforce business rules).

Parameters:
objects - the array to iterate through
Returns:
iterator yields all objects in the array

itermap

public static java.util.Iterator itermap(java.util.Map map,
                                         java.lang.Object[] keys)

join

public static java.lang.String join(java.lang.Object separator,
                                    java.util.Iterator iter)
If you miss Python's join, here it is ;-)

Synopsis

...

Iterator iter = Simple.iterator(new Object[]{"A", "B", "C"});
String joined = Simple.join(", ", iter)
...

Parameters:
separator -
iter -
Returns:
the joined string

join

public static java.lang.StringBuffer join(java.lang.Object separator,
                                          java.util.Iterator iter,
                                          java.lang.StringBuffer sb)
If you miss Python's join, here it is ;-)

Synopsis

...

Iterator iter = Simple.iterator(new Object[]{"A", "B", "C"});
StringBuffer joined = Simple.join(", ", iter, new StringBuffer())
...

Parameters:
separator -
iter -
sb -

password

public static java.lang.String password(int length)

Generate a random password of a given length, using only US ASCII characters.

Parameters:
length - of the password to generate
Returns:
a string of length characters

read

public static java.lang.String read(java.io.BufferedReader br)
                             throws java.io.IOException
Parameters:
br -
Returns:
Throws:
java.io.IOException

read

public static java.lang.String read(java.lang.String name)
Try to read a complete file into a String.

Usage:

String resource = fileRead("my.xml");
Note that since it does not throw exceptions, this method can be used to load static class String members, piggy-backing the class loader to fetch text resources at runtime.

This convenience allows the servlet controller to cache resources when it is initialized.

Parameters:
name - the file's name
Returns:
a String or null

read

public static java.lang.String read(java.net.URL url)
Parameters:
url -
Returns:

recv

public static int recv(java.io.InputStream is,
                       byte[] buffer,
                       int off)
                throws java.io.IOException
Parameters:
is -
buffer -
off -
Returns:
Throws:
java.io.IOException

split

public static java.util.Iterator split(java.lang.String splitted,
                                       char splitter)
Returns an Iterator that splits a string with a single character as fast an lean as possible in Java (without a PCRE and for a maybe too simple use case).

Parameters:
splitted - the String to split
splitter - the char used to split input
Returns:
an Iterator of String