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
|
package org.openslx.satserver.util;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
public class Util
{
/**
* Check if the given string starts with any of the additionally passed strings.
* Ugly boilerplate.
*
* @param stringToCheck a string we want to check the starting of
* @param compareTo list of strings we compare stringToCheck to
* @return true if stringToCheck starts with any of the strings in compareTo
*/
public static boolean startsWith( String stringToCheck, String... compareTo )
{
if ( stringToCheck == null )
return false;
for ( String check : compareTo ) {
if ( stringToCheck.startsWith( check ) )
return true;
}
return false;
}
/**
* Close all given Closables. Can handle null references.
*
* @param streams one or more closables/streams
*/
public static void multiClose( Closeable... streams )
{
if ( streams == null )
return;
for ( Closeable stream : streams ) {
if ( stream != null ) {
try {
stream.close();
} catch ( IOException e ) {
// Ignore - nothing meaningful to do
}
}
}
}
public static boolean streamCopy( InputStream in, OutputStream out, long bytes )
{
byte buffer[] = new byte[ 7900 ];
while ( bytes > 0 ) {
try {
int ret = in.read( buffer, 0, (int) ( bytes > buffer.length ? buffer.length : bytes ) );
if ( ret == -1 )
return false;
bytes -= ret;
out.write( buffer, 0, ret );
} catch ( IOException e ) {
e.printStackTrace();
return false;
}
}
return true;
}
public static String readFileToString( String file ) throws IOException
{
return FileUtils.readFileToString( new File( file ), StandardCharsets.UTF_8 );
}
public static void writeStringToFile( File file, String string ) throws IOException
{
FileUtils.writeStringToFile( file, string, StandardCharsets.UTF_8 );
}
private static final String[] DEFAULT_ALLOWED_DIRS =
{ "/tmp/", "/opt/openslx/configs/" };
public static boolean isAllowedDir( String dir )
{
return startsWith( dir, DEFAULT_ALLOWED_DIRS );
}
private static Pattern nonSpaceExp = Pattern.compile( "[^\\p{C}\\p{Z}]" );
/**
* Whether given string is null, empty, or only matches space-like
* characters.
*/
public static boolean isEmpty( String string )
{
return string == null || !nonSpaceExp.matcher( string ).find();
}
/**
* Parse the given String as a base10 integer.
* If the string does not represent a valid integer, return the given
* default value.
*
* @param value string representation to parse to an int
* @param defaultValue fallback value if given string can't be parsed
* @return
*/
public static int parseInt( String value, int defaultValue )
{
try {
return Integer.parseInt( value );
} catch ( Exception e ) {
return defaultValue;
}
}
}
|