Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.util;

import org.litesoft.commonfoundation.base.*;
import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.core.util.*;

import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.sql.*;
import java.util.*;

@SuppressWarnings({"UnusedDeclaration"})
public class Utils {
    /**
     * True if running on a Mac OS.
     */
    public static final boolean isMac = System.getProperty( "os.name" ).toLowerCase().contains( "mac os x" );

    /**
     * True if running on a Windows OS.
     */
    public static final boolean isWindows = System.getProperty( "os.name" ).toLowerCase().contains( "windows" );

    public static void pause( int pMillisecs )
            throws RuntimeInterruptedException {
        try {
            Thread.sleep( pMillisecs );
        }
        catch ( InterruptedException e ) {
            throw new RuntimeInterruptedException( e );
        }
    }

    public static String getLocalHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        }
        catch ( UnknownHostException e ) {
            return "Unknown";
        }
    }

    public static String getLocalMachineName() {
        String machine = getLocalHostName();
        int firstDot = machine.indexOf( '.' );
        if ( firstDot < 0 ) {
            return machine;
        }
        return machine.substring( 0, firstDot );
    }

    public static String getLocalHostAddress() {
        String zlocalhostAddr;
        try {
            InetAddress zLocalInetAddress = InetAddress.getLocalHost();
            zlocalhostAddr = zLocalInetAddress.getHostAddress();
        }
        catch ( UnknownHostException e ) {
            zlocalhostAddr = "Unknown";
        }
        return zlocalhostAddr;
    }

    /**
     * Find an Available Server Port where pStartSearchWith <= Found Port <= pEndSearchWithbetween.
     *
     * @param pStartSearchWith bottom of search limit (inclusive)
     * @param pEndSearchWith   top of search limit (inclusive)
     *
     * @return !null means the found port, null means none was available in the allowed range
     */
    public static Integer findAvailableServerPort( int pStartSearchWith, int pEndSearchWith ) {
        assertValidServerPort( pStartSearchWith );
        assertValidServerPort( pEndSearchWith );
        for ( int zPort = pStartSearchWith; zPort <= pEndSearchWith; zPort++ ) {
            if ( isServerPortAvailable( zPort ) ) {
                return zPort;
            }
        }
        return null;
    }

    /**
     * Assert that pPort is with the valid range
     *
     * @param pPort must be between 1 and 65535 (inclusive)
     */
    public static void assertValidServerPort( int pPort ) {
        if ( (pPort < 1) || (0xFFFF < pPort) ) {
            throw new IllegalArgumentException( "Invalid Port Number: " + pPort );
        }
    }

    /**
     * Check if pPort is an Available Server Port
     *
     * @param pPort must be between 1 and 65535 (inclusive)
     *
     * @return true if able to create a TCP & Datagram Server Socket on pPort
     */
    public static boolean isServerPortAvailable( int pPort ) {
        assertValidServerPort( pPort );

        ServerSocket ss = null;
        try {
            ss = new ServerSocket( pPort );
            ss.setReuseAddress( true );
        }
        catch ( IOException e ) {
            return false;
        }
        finally {
            if ( ss != null ) {
                try {
                    ss.close();
                }
                catch ( IOException e ) {
                    /* should not be thrown */
                }
            }
        }
        DatagramSocket ds = null;
        try {
            ds = new DatagramSocket( pPort );
            ds.setReuseAddress( true );
        }
        catch ( IOException e ) {
            return false;
        }
        finally {
            if ( ds != null ) {
                ds.close();
            }
        }
        return true;
    }

    public static String dirPath( String pPath ) {
        if ( pPath != null ) {
            if ( new File( pPath ).isDirectory() ) {
                if ( !Paths.endsWithSep( pPath ) ) {
                    pPath += "/";
                }
            } else {
                int at = Paths.lastSepAt( pPath );
                if ( at == -1 ) {
                    pPath = "./";
                } else {
                    pPath = pPath.substring( 0, at + 1 );
                }
            }
        }
        return pPath;
    }

    public static String relativePath( String pBasePath, String pPossibleRelativePath ) {
        if ( (pPossibleRelativePath == null) || (pPossibleRelativePath.indexOf( ':' ) != -1) || Characters.isPathSep( pPossibleRelativePath.charAt( 0 ) ) ) {
            return pPossibleRelativePath;
        }
        return dirPath( pBasePath ) + pPossibleRelativePath;
    }

    public static boolean isNoise( Throwable pThrowable ) {
        return (pThrowable instanceof UndeclaredThrowableException) || //
               (pThrowable instanceof InvocationTargetException);
    }

    public static List<Throwable> getAllThrowables( Throwable pThrowable ) {
        ArrayList<Throwable> rv = new ArrayList<Throwable>();
        if ( pThrowable != null ) {
            addAll( rv, pThrowable );
        }
        return rv;
    }

    private static void addAll( List<Throwable> pAll, Throwable pThrowable ) {
        while ( pThrowable != null ) {
            if ( pAll.contains( pThrowable ) ) {
                return;
            }
            pAll.add( pThrowable );
            if ( pThrowable instanceof SQLException ) {
                addAll( pAll, ((SQLException) pThrowable).getNextException() );
            }
            pThrowable = pThrowable.getCause();
        }
    }

    /**
     * Create an OutputStream from the pOutputStreamFactory and after copying the data from the pInputStream to the created OutputStream, close both.
     *
     * @param pInputStream         if !null data copied to OutputStream created by pOutputStreamFactory and will be closed!
     * @param pOutputStreamFactory if null will throw null pointer exception, otherwise the OutputStream it creates will be closed!
     */
    public static void copy( InputStream pInputStream, OutputStreamFactory pOutputStreamFactory )
            throws IOException {
        OutputStream zOutputStream = null;
        try {
            zOutputStream = pOutputStreamFactory.createOutputStream();
            if ( pInputStream != null ) {
                byte[] buffer = new byte[4096];
                for ( int bytesRead; -1 != (bytesRead = pInputStream.read( buffer )); ) {
                    if ( bytesRead > 0 ) {
                        zOutputStream.write( buffer, 0, bytesRead );
                    }
                }
                InputStream zIS = pInputStream;
                pInputStream = null;
                zIS.close();
            }
            zOutputStream.flush();
            OutputStream zOS = zOutputStream;
            zOutputStream = null;
            zOS.close();
        }
        finally {
            dispose( pInputStream );
            dispose( zOutputStream );
        }
    }

    public static void dispose( Closeable pCloseable ) {
        if ( pCloseable != null ) {
            try {
                pCloseable.close();
            }
            catch ( IOException e ) {
                // Whatever
            }
        }
    }

    public static BufferedReader getBufferedReader( Reader pReader ) {
        if ( pReader == null ) {
            return null;
        }
        if ( pReader instanceof BufferedReader ) {
            return (BufferedReader) pReader;
        }
        return new BufferedReader( pReader );
    }

    public static <T, U> Map<T, U> addKeyValuesTo( Map<T, U> pMap, Object... pProperty_NameValues )
            throws IllegalArgumentException {
        if ( Currently.isNotNullOrEmpty( pProperty_NameValues ) ) {
            if ( (pProperty_NameValues.length & 1) != 0 ) {
                throw new IllegalArgumentException( "Attempt to add Properties that were NOT Name/Value pairs" );
            }
            for ( int i = 0; i < pProperty_NameValues.length; i += 2 ) {
                put( pMap, pProperty_NameValues[i], pProperty_NameValues[i + 1] );
            }
        }
        return pMap;
    }

    @SuppressWarnings({"unchecked"})
    private static <T, U> void put( Map<T, U> pMap, Object pKey, Object pValue ) {
        pMap.put( (T) pKey, (U) pValue );
    }

    public static String toStringEmptyIfNullOrAppend( Object pPrintObject, String pAppend ) {
        if ( pPrintObject == null ) {
            return "";
        }
        return toString( pPrintObject ) + pAppend;
    }

    public static String toStringEmptyIfNullOrPrependAndAppend( String pPrepend, Object pPrintObject, String pAppend ) {
        if ( pPrintObject == null ) {
            return "";
        }
        return pPrepend + toString( pPrintObject ) + pAppend;
    }

    public static String toString( Object pObject ) {
        StringBuilder sb = new StringBuilder();
        sbAppend( sb, pObject );
        return sb.toString();
    }

    public static void sbAppend( StringBuilder pSB, Object pObject ) {
        if ( pObject == null ) {
            pSB.append( "null" );
        } else if ( pObject.getClass().isArray() ) {
            sbAppendArrayObject( pSB, pObject );
        } else if ( pObject instanceof Collection ) {
            sbAppend( pSB, (Collection<?>) pObject );
        } else if ( pObject instanceof Map ) {
            sbAppend( pSB, (Map<?, ?>) pObject );
        } else // Default (Last Resort)...
        {
            pSB.append( pObject.toString() );
        }
    }

    private static void sbAppend( StringBuilder pSB, Collection<?> pCollection ) {
        pSB.append( '[' );
        sbAppendArray( pSB, pCollection.toArray() );
        pSB.append( ']' );
    }

    private static void sbAppend( StringBuilder pSB, Map<?, ?> pMap ) {
        pSB.append( '{' );

        Object[] keys = pMap.keySet().toArray();
        if ( keys.length != 0 ) {
            sbAppendMapEntry( pSB, pMap, keys[0] );
            for ( int i = 1; i < keys.length; i++ ) {
                pSB.append( ", " );
                sbAppendMapEntry( pSB, pMap, keys[i] );
            }
        }
        pSB.append( '}' );
    }

    private static void sbAppendMapEntry( StringBuilder pSB, Map<?, ?> pMap, Object pKey ) {
        sbAppend( pSB, pKey );
        pSB.append( '=' );
        //noinspection SuspiciousMethodCalls
        sbAppend( pSB, pMap.get( pKey ) );
    }

    private static void sbAppendArrayObject( StringBuilder pSB, Object pObject ) {
        pSB.append( '<' );
        if ( pObject instanceof Object[] ) {
            sbAppendArray( pSB, (Object[]) pObject );
        } else if ( pObject instanceof byte[] ) {
            sbAppendArray( pSB, (byte[]) pObject );
        } else if ( pObject instanceof char[] ) {
            sbAppendArray( pSB, (char[]) pObject );
        } else if ( pObject instanceof double[] ) {
            sbAppendArray( pSB, (double[]) pObject );
        } else if ( pObject instanceof float[] ) {
            sbAppendArray( pSB, (float[]) pObject );
        } else if ( pObject instanceof int[] ) {
            sbAppendArray( pSB, (int[]) pObject );
        } else if ( pObject instanceof long[] ) {
            sbAppendArray( pSB, (long[]) pObject );
        } else if ( pObject instanceof short[] ) {
            sbAppendArray( pSB, (short[]) pObject );
        } else if ( pObject instanceof boolean[] ) {
            sbAppendArray( pSB, (boolean[]) pObject );
        } else // Default (Last Resort)...
        {
            pSB.append( '?' );
            pSB.append( pObject );
            pSB.append( '?' );
        }
        pSB.append( '>' );
    }

    private static void sbAppendArray( StringBuilder pSB, Object[] pObjects ) {
        if ( pObjects.length != 0 ) {
            sbAppend( pSB, pObjects[0] );
            for ( int i = 1; i < pObjects.length; i++ ) {
                pSB.append( ", " );
                sbAppend( pSB, pObjects[i] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, byte[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, char[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, double[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, float[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, int[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, long[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, short[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    private static void sbAppendArray( StringBuilder pSB, boolean[] pPrimitives ) {
        if ( pPrimitives.length != 0 ) {
            pSB.append( pPrimitives[0] );
            for ( int i = 1; i < pPrimitives.length; i++ ) {
                pSB.append( ',' );
                pSB.append( pPrimitives[0] );
            }
        }
    }

    public static String fixPercentsInURLs( String pURLtext ) {
        if ( pURLtext != null ) {
            int zFrom = 0;
            for ( int zAt; -1 != (zAt = pURLtext.indexOf( '%', zFrom )); zFrom = zAt + 1 ) {
                if ( pURLtext.length() <= (zAt + 2) ) {
                    break;
                }
                int zHi = Hex.fromChar( pURLtext.charAt( zAt + 1 ) );
                int zLo = Hex.fromChar( pURLtext.charAt( zAt + 2 ) );
                if ( (zHi != -1) && (zLo != -1) ) {
                    pURLtext = pURLtext.substring( 0, zAt ) + //
                               ((char) ((zHi * 16) + zLo)) + //
                               pURLtext.substring( zAt + 3 );
                }
            }
        }
        return pURLtext;
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/util/Utils.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

942 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:41:46 +0000

Extracting commonfoundation

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

906 Diff Diff GeorgeS picture GeorgeS Fri 07 Jun, 2013 22:50:38 +0000

Update to latest gwt-phonegap & MGWT.

858 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 18:40:40 +0000
823 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 16:10:13 +0000
822 GeorgeS picture GeorgeS Sun 19 Aug, 2012 01:03:51 +0000