Methods Summary |
---|
protected java.lang.String | decode(byte[] data)
if ( data == null ){
return( null );
}
StringBuffer res = new StringBuffer( data.length*2 );
for (int i=0;i<data.length;i++){
byte c = data[i];
if ( VALID_CHARS.indexOf( Character.toLowerCase((char)c)) != -1 ){
res.append((char)c);
}else{
res.append( "_" );
res.append( ByteFormatter.nicePrint(c));
}
}
// more often that not these decoded values are used for filenames. Windows has a limit
// of 250 (ish) chars, so we do something sensible with longer values
int len = res.length();
if ( len > max_ok_name_length ){
// could be a file system out there that supports arbitarily long names, so
// we can't pre-calculate the max
if ( ( !max_ok_name_length_determined )&&
fileLengthOK( len )){
// this length is ok, bump up the known limit
max_ok_name_length = len;
}else{
// won't fit
if ( !max_ok_name_length_determined ){
for (int i=max_ok_name_length+16;i<len;i+=16 ){
if ( fileLengthOK( i )){
max_ok_name_length = i;
}else{
break;
}
}
max_ok_name_length_determined = true;
}
// try and preserve extension
String extension = null;
int pos = res.lastIndexOf(".");
if ( pos != -1 ){
// include the "."
extension = res.substring( pos );
if ( extension.length() == 1 || extension.length() > 4 ){
extension = null;
}
}
// replace the end of the string with a hash value to ensure uniqueness
byte[] hash = new SHA1Hasher().calculateHash( data );
String hash_str = ByteFormatter.nicePrint( hash, true );
res = new StringBuffer(res.substring(
0,
max_ok_name_length - hash_str.length() - (extension == null?0:extension.length())));
res.append( hash_str );
if ( extension != null ){
res.append( extension );
}
}
}
return( res.toString());
|
public java.lang.String | decodeString(byte[] bytes)
return( decode( bytes ));
|
protected boolean | fileLengthOK(int len)
StringBuffer n = new StringBuffer( len );
for (int i=0;i<len;i++){
n.append( "A" );
}
try{
File f = File.createTempFile( n.toString(), "" );
f.delete();
return( true );
}catch( Throwable e ){
return( false );
}
|
public int | getIndex()
return( index );
|
public java.lang.String | getName()
return( NAME );
|
public java.lang.String | tryDecode(byte[] bytes, boolean lax)
return( decode( bytes ));
|