com.sap.ip.me.api.services
Class IOUtils

java.lang.Object
  extended bycom.sap.ip.me.api.services.IOUtils

public class IOUtils
extends java.lang.Object

This class contains some useful static methods for IO-operations.

Author:
SAP

Field Summary
static java.lang.String DEFAULT_CHARACTER_ENCODING
          Default character encoding for the ME while writing or reading characters from Streams
static java.io.InputStream termInputStream
          Field to terminate connection
 
Constructor Summary
IOUtils()
           
 
Method Summary
static boolean checkInputStream()
          Checks whether InputStream that is sent to the URL is null or not
static boolean closeInputStream()
          Closes the InputStream that is sent to the URL.
static void copyFile(java.io.File source, java.io.File target, int bufferLength)
          Copies the content of the source to the target file in portions of length defined by bufferLength
static void copyStreams(java.io.InputStream in, java.io.OutputStream out)
          Copys the input stream into the output stream.
static void copyStreams(java.io.InputStream in, java.io.OutputStream out, boolean closeInputStream, boolean closeOutputStream)
          Copys the input stream into the output stream.
static boolean deleteDirectoryRecursively(java.io.File dir)
          Deletes a directory structure recusively including all files within the given directory and within all subdirectories.
static boolean deleteDirectoryRecursively(java.io.File dir, boolean deleteRootFolder)
          Deletes a directory structure recusively including all files within the given directory and within all subdirectories.
static java.io.File extractZipFile(java.util.zip.ZipFile zip, java.util.zip.ZipEntry sourceEntry, java.lang.String targetFilename)
          Extract a ZipEntry out of a ZipFile.
static long getTimeBetweenStreamCopy()
          This methods returns the time that has been spent on copying streams since last reset.
static void httpGet(java.lang.String sUrl, java.io.OutputStream out)
          Sends a http get request to the given url.
static void httpGet(java.lang.String sUrl, java.io.OutputStream out, boolean compressed)
          Sends a http get request to the given url.
static void httpGet(java.lang.String sUrl, java.lang.String destination)
          Sends a http get request to the given url.
static void httpGet(java.lang.String sUrl, java.lang.String destination, boolean compressed)
          Sends a http get request to the given url.
static void httpPost(java.lang.String sUrl, int len, java.io.InputStream in, java.io.OutputStream out)
          Wrapps the given inputstream into a http post request to the given url and writes the response into the given outputstream.
static void httpPost(java.lang.String sUrl, int len, java.io.InputStream in, java.io.OutputStream out, boolean compressed)
          Wrapps the given inputstream into a http post request to the given url and writes the response into the given outputstream.
static void httpPost(java.lang.String sUrl, java.lang.String source, java.lang.String destination)
          Sends a http post request to the given url.
static void httpPost(java.lang.String sUrl, java.lang.String source, java.lang.String destination, boolean compressed)
          Sends a http post request to the given url.
static void isConnectable(java.net.URL url)
          Checks if a connection can be established to the given url.
static void isConnectable(java.net.URL url, java.lang.String userName, java.lang.String password, java.lang.String ssoTicket)
          Checks if a connection can be established to the given url.
static boolean moveFileToDirectory(java.io.File sourceFile, java.io.File targetDirectory)
          This method moves a file from it's original location to another directory.
static java.util.Hashtable readHashtableFromDirectory(java.io.File directory)
          creates hashtable from the given directory, from files, which are written to this directory via saveHashtableToDirectory(File directory, Hashtable hashtable) method.
static java.lang.Object readSerializedObjectFromFile(java.io.File file)
          Reads a serialized object from the given file.
static java.util.Vector readSerializedVectorFromFile(java.io.File file)
          Reads a serialized vector from the given file.
static void resetTimeBetweenStreamCopy()
          This methods resets the counter that is used to measure the time spent on copyStreams.
static void saveHashtableToDirectory(java.io.File directory, java.util.Hashtable hashtable)
          Saves this part of the given hashtable object, which can be serialized, to the given directory and ignores the rest.
static void serializeObjectToFile(java.io.File file, java.lang.Object object)
          Serializes the given object to the given file.
static void serializeVectorToFile(java.io.File file, java.util.Vector vector)
          Serializes the given vector object to the given file.
static void setProxyAuthorization(java.net.URLConnection urlC)
          Set the request property "Proxy-Authorization" for a given URLConnection .
static java.lang.String utf8AndUrlDecode(byte[] source, int offset, int len)
          Decode a byte array from the "x-www-form-urlencoded" form, enhanced with the UTF-8-in-URL proposal.
static java.lang.String utf8AndUrlEncode(java.lang.String s)
          Encode a string to the "x-www-form-urlencoded" form, enhanced with the UTF-8-in-URL proposal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARACTER_ENCODING

public static final java.lang.String DEFAULT_CHARACTER_ENCODING
Default character encoding for the ME while writing or reading characters from Streams

See Also:
Constant Field Values

termInputStream

public static java.io.InputStream termInputStream
Field to terminate connection

Constructor Detail

IOUtils

public IOUtils()
Method Detail

utf8AndUrlEncode

public static final java.lang.String utf8AndUrlEncode(java.lang.String s)
Encode a string to the "x-www-form-urlencoded" form, enhanced with the UTF-8-in-URL proposal. This is what happens:

Parameters:
s - The string to be encoded.
Returns:
The encoded string.

utf8AndUrlDecode

public static final java.lang.String utf8AndUrlDecode(byte[] source,
                                                      int offset,
                                                      int len)
Decode a byte array from the "x-www-form-urlencoded" form, enhanced with the UTF-8-in-URL proposal. The reverse method for utf8AndUrlEncode .

Parameters:
source - The byte array to be decoded.
offset - Description of the Parameter
len - Description of the Parameter
Returns:
The decoded String.

setProxyAuthorization

public static void setProxyAuthorization(java.net.URLConnection urlC)
                                  throws java.io.UnsupportedEncodingException
Set the request property "Proxy-Authorization" for a given URLConnection .
The proxy user and the proxy password will be read from a file called proxyauth.txt
This file must reside in the installation directory of Mobile Engine. After that, a Base64 encoding will be done.

Parameters:
urlC - The URLConnection
Throws:
java.io.UnsupportedEncodingException - Description of the Exception
See Also:
Configuration.getInstallationDirectory()

httpPost

public static void httpPost(java.lang.String sUrl,
                            int len,
                            java.io.InputStream in,
                            java.io.OutputStream out,
                            boolean compressed)
                     throws HttpConnectionException
Wrapps the given inputstream into a http post request to the given url and writes the response into the given outputstream. The streams have to be opened and this method will close the streams if it succeeds.

Parameters:
sUrl - The url for the http post request.
len - The length of the stream that will be wrapped within the request.
in - The stream that should be send to the url. This stream has to be opened at method call and after sending the data this stream will be closed.
out - The response of the request will be written to this stream. This stream has to be opened at method call and after getting the response this stream will be closed.
compressed - Indicates whether the transfer of the streams use compression or not.
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

httpPost

public static void httpPost(java.lang.String sUrl,
                            int len,
                            java.io.InputStream in,
                            java.io.OutputStream out)
                     throws HttpConnectionException
Wrapps the given inputstream into a http post request to the given url and writes the response into the given outputstream. The streams have to be opened and this method will close the streams if it succeeds. All transfers are uncompressed.

Parameters:
sUrl - The url for the http post request.
len - Description of the Parameter
in - Description of the Parameter
out - Description of the Parameter
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

httpPost

public static void httpPost(java.lang.String sUrl,
                            java.lang.String source,
                            java.lang.String destination,
                            boolean compressed)
                     throws HttpConnectionException
Sends a http post request to the given url. The given source file will be wrapped into the request. The response will be saved within the given destination file.

Parameters:
sUrl - The target url.
source - The source filename.
destination - The file name for the response.
compressed - Indicates whether the transfer uses compression or not.
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

httpPost

public static void httpPost(java.lang.String sUrl,
                            java.lang.String source,
                            java.lang.String destination)
                     throws HttpConnectionException
Sends a http post request to the given url. The given source file will be wrapped into the request. The response will be saved within the given destination file. Transfer does not use compression.

Parameters:
sUrl - The target url.
source - The source filename.
destination - The file name for the response.
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

httpGet

public static void httpGet(java.lang.String sUrl,
                           java.lang.String destination,
                           boolean compressed)
                    throws HttpConnectionException
Sends a http get request to the given url. The response will be saved within the given destination file.

Parameters:
sUrl - The target url.
destination - The file name for the response.
compressed - Indicates whether the transfer uses compression or not.
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

httpGet

public static void httpGet(java.lang.String sUrl,
                           java.lang.String destination)
                    throws HttpConnectionException
Sends a http get request to the given url. The response will be saved within the given destination file. Does not use compression while transfer.

Parameters:
sUrl - The target url.
destination - The file name for the response.
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

httpGet

public static void httpGet(java.lang.String sUrl,
                           java.io.OutputStream out,
                           boolean compressed)
                    throws HttpConnectionException
Sends a http get request to the given url. The response will be saved within the given destination file.

Parameters:
sUrl - The target url.
out - The response of the request will be written to this stream. This stream has to be opened at method call and after getting the response this stream will be closed.
compressed - Indicates whether the transfer uses compression or not.
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

httpGet

public static void httpGet(java.lang.String sUrl,
                           java.io.OutputStream out)
                    throws HttpConnectionException
Sends a http get request to the given url. The response will be saved within the given destination file. Does not use compression while transfer.

Parameters:
sUrl - The target url.
out - The response of the request will be written to this stream. This stream has to be opened at method call and after getting the response this stream will be closed.
Throws:
HttpConnectionException - If there are any IO exceptions while transfering data a HttpConnectionException will be thrown.

isConnectable

public static void isConnectable(java.net.URL url)
                          throws HttpConnectionException
Checks if a connection can be established to the given url. If no exception is thrown the connection was successful. If the SYNCSETTINGS_CONNECTION_TIMEOUT is set to -1 the connection test will be ignored.

Parameters:
url - The destination url.
Throws:
HttpConnectionException - If the connection test was not successful a HttpConnectionException will be thrown.

isConnectable

public static void isConnectable(java.net.URL url,
                                 java.lang.String userName,
                                 java.lang.String password,
                                 java.lang.String ssoTicket)
                          throws HttpConnectionException
Checks if a connection can be established to the given url. If no exception is thrown the connection was successful. If the SYNCSETTINGS_CONNECTION_TIMEOUT is set to -1 the connection test will be ignored.
Given parameters are used for authentication. If sso ticket is not null MYSAPSSO2 cockie will be set. Otherwise if password is not null sap-password will be set to given password and sap-user will be set to given username.

Parameters:
url - The destination url.
userName - User name for authentication.
password - Password for authentication.
ssoTicket - MYSAPSSO2 ticket for authtentication.
Throws:
HttpConnectionException - If the connection test was not successful a HttpConnectionException will be thrown.

copyStreams

public static void copyStreams(java.io.InputStream in,
                               java.io.OutputStream out)
                        throws java.io.IOException
Copys the input stream into the output stream. The streams have to be open. After the copying the streams will be closed.

Parameters:
in - The input stream.
out - The output stream.
Throws:
java.io.IOException

copyStreams

public static void copyStreams(java.io.InputStream in,
                               java.io.OutputStream out,
                               boolean closeInputStream,
                               boolean closeOutputStream)
                        throws java.io.IOException
Copys the input stream into the output stream. The streams have to be open. After the copying the streams will be closed.

Parameters:
in - The input stream.
out - The output stream.
closeInputStream - true iff the input stream should be closed after copying.
closeOutputStream - true iff the output stream should be closed after copying.
Throws:
java.io.IOException

extractZipFile

public static java.io.File extractZipFile(java.util.zip.ZipFile zip,
                                          java.util.zip.ZipEntry sourceEntry,
                                          java.lang.String targetFilename)
                                   throws java.io.IOException
Extract a ZipEntry out of a ZipFile. The target filename is also given. The method returns a File handle to the extracted file. If there is an error while extracting a ZipEntry an IOException will be thrown.

Parameters:
zip - The source ZipFile.
sourceEntry - The ZipEntry that sould be extracted.
targetFilename - The target filename.
Returns:
A File handle of the extracted file.
Throws:
java.io.IOException - IOException will be thrown if any error occurs.

serializeVectorToFile

public static void serializeVectorToFile(java.io.File file,
                                         java.util.Vector vector)
Serializes the given vector object to the given file. If one of the arguments is null nothing will be done.

Parameters:
file - The destination file.
vector - The vector object that has to be serialized.

saveHashtableToDirectory

public static void saveHashtableToDirectory(java.io.File directory,
                                            java.util.Hashtable hashtable)
Saves this part of the given hashtable object, which can be serialized, to the given directory and ignores the rest.
Each value will be saved separately. That means if one of the keys/values can not be serialized, only this one will be skipped and not the whole hashtable. The implementation is not optimized and should be used only for very small hashtables. Example: two values are contained in the hashtable and the first one is not serializable. In this case the saved hashtable contains only the second value.

Parameters:
directory - The destination directory.
hashtable - The hashtable that has to be saved.
See Also:
readHashtableFromDirectory

readHashtableFromDirectory

public static java.util.Hashtable readHashtableFromDirectory(java.io.File directory)
creates hashtable from the given directory, from files, which are written to this directory via saveHashtableToDirectory(File directory, Hashtable hashtable) method. In other case an empty hasshtable will be returned

Parameters:
directory - The destination directory.
Returns:
Description of the Return Value
See Also:
saveHashtableToDirectory

serializeObjectToFile

public static void serializeObjectToFile(java.io.File file,
                                         java.lang.Object object)
Serializes the given object to the given file. If one of the arguments is null nothing will be done.

Parameters:
file - The destination file.
object - The object that has to be serialized.

readSerializedVectorFromFile

public static java.util.Vector readSerializedVectorFromFile(java.io.File file)
Reads a serialized vector from the given file.

Parameters:
file - The file.
Returns:
The deserialized vector read from file. If there are any errors an empty vector will be returned.

readSerializedObjectFromFile

public static java.lang.Object readSerializedObjectFromFile(java.io.File file)
Reads a serialized object from the given file.

Parameters:
file - The file.
Returns:
The deserialized object read from file. If there are any errors null will be returned.

copyFile

public static void copyFile(java.io.File source,
                            java.io.File target,
                            int bufferLength)
                     throws java.io.IOException
Copies the content of the source to the target file in portions of length defined by bufferLength

Parameters:
source - Source file
target - Target file
bufferLength - The size of buffer in character which will be used for the copy
Throws:
java.io.IOException - see

deleteDirectoryRecursively

public static boolean deleteDirectoryRecursively(java.io.File dir)
Deletes a directory structure recusively including all files within the given directory and within all subdirectories. The directories will be deleted also.

Parameters:
dir - The file or directory that has to be deleted.
Returns:
true iff the deletion was successful.

deleteDirectoryRecursively

public static boolean deleteDirectoryRecursively(java.io.File dir,
                                                 boolean deleteRootFolder)
Deletes a directory structure recusively including all files within the given directory and within all subdirectories. The directories will be deleted also.

Parameters:
dir - The file or directory that has to be deleted.
deleteRootFolder - true if dir should be deleted too.
Returns:
true iff the deletion was successful.

getTimeBetweenStreamCopy

public static long getTimeBetweenStreamCopy()
This methods returns the time that has been spent on copying streams since last reset.

Returns:
long The time in milliseconds that has been spent on copying streams since last reset of the counter.

resetTimeBetweenStreamCopy

public static void resetTimeBetweenStreamCopy()
This methods resets the counter that is used to measure the time spent on copyStreams.


moveFileToDirectory

public static boolean moveFileToDirectory(java.io.File sourceFile,
                                          java.io.File targetDirectory)
This method moves a file from it's original location to another directory. The source file is deleted in it's original location.

Parameters:
sourceFile - The original file
targetDirectory - The directory to which the sourceFile will be moved
Returns:
trueif the move was successfull, false if the move was not successfull, if one of the two parameters was null, if targetDirectory.isFile() or if sourceFile.isDirectory()

closeInputStream

public static boolean closeInputStream()
Closes the InputStream that is sent to the URL. This overrides the WaitForResultsTimeout value set in the Mobile Engine configuration settings.

Returns:
true if InputStream was closed successfully false otherwise.

checkInputStream

public static boolean checkInputStream()
Checks whether InputStream that is sent to the URL is null or not

Returns:
true if InputStream is not null false otherwise.


Copyright © 2005 SAP AG. All Rights Reserved.