public class SampUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
LOCALHOST_PROP
Property which can be used to set name used for localhost in server
endpoints.
|
private static java.io.File |
lockFile_ |
private static java.util.logging.Logger |
logger_ |
private static java.lang.String |
NEWLINE |
private static java.lang.String |
sampVersion_ |
private static java.lang.String |
softwareVersion_ |
Modifier | Constructor and Description |
---|---|
private |
SampUtils()
Private constructor prevents instantiation.
|
Modifier and Type | Method and Description |
---|---|
static void |
checkList(java.util.List list)
Checks that a given List is legal for use in a SAMP context.
|
static void |
checkMap(java.util.Map map)
Checks that a given Map is legal for use in a SAMP context.
|
static void |
checkObject(java.lang.Object obj)
Checks that a given object is legal for use in a SAMP context.
|
static void |
checkString(java.lang.String string)
Checks that a given String is legal for use in a SAMP context.
|
static void |
checkUrl(java.lang.String url)
Checks that a string is a legal URL.
|
static boolean |
decodeBoolean(java.lang.String s)
Returns the boolean value for a SAMP boolean string.
|
static double |
decodeFloat(java.lang.String s)
Returns the double value for a SAMP float string.
|
static int |
decodeInt(java.lang.String s)
Returns the integer value for a SAMP int string.
|
static long |
decodeLong(java.lang.String s)
Returns the integer value as a
long for a SAMP int
string. |
static java.lang.String |
encodeBoolean(boolean b)
Returns a SAMP boolean string representation of a boolean value.
|
static java.lang.String |
encodeFloat(double d)
Returns a SAMP float string representation of a floating point
value.
|
static java.lang.String |
encodeInt(int i)
Returns a SAMP int string representation of an integer.
|
static java.lang.String |
encodeLong(long i)
Returns a SAMP int string representation of a long integer.
|
static java.net.URL |
fileToUrl(java.io.File file)
Turns a File into a URL.
|
private static int |
findAnyPort()
Locates an unused server port on the local host.
|
static java.lang.String |
formatObject(java.lang.Object obj,
int indent)
Pretty-prints a SAMP object.
|
static java.lang.Object |
fromJson(java.lang.String str)
Parses JSON text to give a SAMP object.
|
private static java.lang.String |
getLineSeparator()
Returns the system-dependent line separator sequence.
|
static java.lang.String |
getLocalhost()
Returns a string denoting the local host to be used for communicating
local server endpoints and so on.
|
static java.lang.String |
getSampVersion()
Returns a string giving the version of the SAMP standard which this
software implements.
|
static java.lang.String |
getSoftwareVersion()
Returns a string giving the version of this software package.
|
static int |
getUnusedPort(int startPort)
Returns an unused port number on the local host.
|
static boolean |
isStringChar(char c)
Indicates whether a given character is legal to include in a SAMP
string.
|
static java.lang.Object |
parseValue(java.lang.String str)
Parses a command-line string as a SAMP object.
|
private static java.lang.String |
readResource(java.lang.String rname)
Returns the contents of a resource as a string.
|
private static java.lang.String |
replaceChar(java.lang.String in,
char oldChar,
java.lang.String newTxt)
Replaces all occurrences of a single character with a given replacement
string.
|
private static int |
scanForPort(int startPort,
int nTry)
Two problems with this one - it may be a bit inefficient, and
there's an annoying bug in the Apache XML-RPC WebServer class
which causes it to print "java.util.NoSuchElementException" to
the server's System.err for every port scanned by this routine
that an org.apache.xmlrpc.WebServer server is listening on.
|
static java.lang.String |
toJson(java.lang.Object item,
boolean multiline)
Serializes a SAMP object to a JSON string.
|
static java.lang.String |
toString(Client client)
Returns a string representation of a client object.
|
static java.lang.String |
uriDecode(java.lang.String text)
Reverses URI-style character escaping (%xy) on a string.
|
static java.lang.String |
uriEncode(java.lang.String text)
Performs URI-style character escaping (%xy) on a string.
|
static java.io.File |
urlToFile(java.net.URL url)
Attempts to interpret a URL as a file.
|
public static final java.lang.String LOCALHOST_PROP
getLocalhost()
,
Constant Field Valuesprivate static final java.util.logging.Logger logger_
private static java.lang.String sampVersion_
private static java.lang.String softwareVersion_
private static java.io.File lockFile_
private static final java.lang.String NEWLINE
public static java.lang.String encodeInt(int i)
i
- integer valuepublic static int decodeInt(java.lang.String s)
s
- SAMP int stringjava.lang.NumberFormatException
- if conversion failspublic static java.lang.String encodeLong(long i)
i
- integer valuepublic static long decodeLong(java.lang.String s)
long
for a SAMP int
string.s
- SAMP int stringjava.lang.NumberFormatException
- if conversion failspublic static java.lang.String encodeFloat(double d)
d
- double valuejava.lang.IllegalArgumentException
- if d
is NaN or infinitepublic static double decodeFloat(java.lang.String s)
s
- SAMP float stringjava.lang.NumberFormatException
- if conversion failspublic static java.lang.String encodeBoolean(boolean b)
b
- boolean valuepublic static boolean decodeBoolean(java.lang.String s)
s
- SAMP boolean strings
is equal to zeropublic static void checkObject(java.lang.Object obj)
obj
- object to checkDataException
- in case of an errorpublic static void checkMap(java.util.Map map)
map
- map to checkDataException
- in case of an errorcheckObject(java.lang.Object)
public static void checkList(java.util.List list)
list
- list to checkDataException
- in case of errorcheckObject(java.lang.Object)
public static void checkString(java.lang.String string)
string
- string to checkDataException
- in case of errorpublic static boolean isStringChar(char c)
public static void checkUrl(java.lang.String url)
url
- string to checkDataException
- if url
is not a legal URLpublic static java.lang.String toString(Client client)
client
- client objectpublic static java.lang.String formatObject(java.lang.Object obj, int indent)
obj
- SAMP-friendly objectindent
- base indent for text blockpublic static java.lang.Object parseValue(java.lang.String str)
str
- command-line argumentpublic static java.lang.String getLocalhost()
The value returned by default is the loopback address, "127.0.0.1".
However this behaviour can be overridden by setting the
LOCALHOST_PROP
system property to the string which should
be returned instead.
This may be necessary if the loopback address is not appropriate,
for instance in the case of multiple configured loopback interfaces(?)
or where SAMP communication is required across different machines.
There are two special values which may be used for this property:
[hostname]
:
uses the fully qualified domain name of the host[hostnumber]
:
uses the IP number of the hostIn JSAMP version 0.3-1 and prior versions, the [hostname] behaviour was the default. Although this might be seen as more correct, in practice it could cause a lot of problems with DNS configurations which are incorrect or unstable (common in laptops outside their usual networks). See, for instance, AstroGrid bugzilla tickets 1799, 2151.
In JSAMP version 0.3-1 and prior versions, the property was
named samp.localhost
rather than
jsamp.localhost
. This name is still accepted for
backwards compatibility.
public static int getUnusedPort(int startPort) throws java.io.IOException
startPort
- suggested port number; may or may not be usedjava.io.IOException
public static java.net.URL fileToUrl(java.io.File file)
file://localhost/abs-path
" rather than
"file:abs-or-rel-path
".file
- filepublic static java.lang.String uriDecode(java.lang.String text)
URLDecoder
,
this does not turn "+" characters into spaces.text
- escaped textpublic static java.lang.String uriEncode(java.lang.String text)
URLEncoder
,
this encodes spaces as "%20" and not "+".text
- unescaped textpublic static java.io.File urlToFile(java.net.URL url)
url
- URL, may or may not be file: protocolpublic static java.lang.Object fromJson(java.lang.String str)
str
- string to parsepublic static java.lang.String toJson(java.lang.Object item, boolean multiline)
item
- to serializemultiline
- true for formatted multiline output, false for a
single linepublic static java.lang.String getSampVersion()
public static java.lang.String getSoftwareVersion()
private static java.lang.String readResource(java.lang.String rname)
rname
- resource name
(in the sense of Class.getResource(java.lang.String)
)private static java.lang.String getLineSeparator()
private static java.lang.String replaceChar(java.lang.String in, char oldChar, java.lang.String newTxt)
in
- input stringoldChar
- character to replacenewText
- replacement stringprivate static int findAnyPort() throws java.io.IOException
java.io.IOException
private static int scanForPort(int startPort, int nTry) throws java.io.IOException
startPort
- port to start scanning upwards fromnTry
- number of ports in sequence to try before admitting defeatjava.io.IOException