Last active
April 13, 2016 02:50
-
-
Save joeyjiron06/79ee6057780ffc0b681f to your computer and use it in GitHub Desktop.
A utility class for java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JAVA UTILS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileUtils { | |
public static void copyInputStreamToFile( InputStream in, File file ) { | |
try { | |
OutputStream out = new FileOutputStream(file); | |
byte[] buf = new byte[1024]; | |
int len; | |
while((len=in.read(buf))>0){ | |
out.write(buf,0,len); | |
} | |
out.close(); | |
in.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static String safeReadFile(String path, Charset encoding){ | |
File file = new File(path); | |
StringBuilder fileContents = new StringBuilder((int)file.length()); | |
Scanner scanner = null; | |
String lineSeparator = System.getProperty("line.separator"); | |
try { | |
scanner = new Scanner(file); | |
while(scanner.hasNextLine()) { | |
fileContents.append(scanner.nextLine()); | |
if (scanner.hasNextLine()) { | |
fileContents.append(lineSeparator); | |
} | |
} | |
return fileContents.toString(); | |
} | |
catch (FileNotFoundException e) { | |
System.out.println("file not found!"); | |
} | |
finally { | |
if (scanner != null) { | |
scanner.close(); | |
} | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Utils { | |
private Utils() { } | |
// C H E C K E R S | |
public static void nonNull(Object object) { | |
if (object == null) { | |
throw new NullPointerException(); | |
} | |
} | |
public static void nonNullOfEmpty(Collection<?> collection) { | |
if (collection == null) { | |
throw new NullPointerException(); | |
} | |
if (collection.isEmpty()) { | |
throw new IllegalArgumentException(); | |
} | |
} | |
public static boolean isNullOrEmpty(String string) { | |
return string == null || "".equals(string.trim()); | |
} | |
public static boolean isNullOrEmpty(Collection<?> collection) { | |
return collection == null || collection.isEmpty(); | |
} | |
public static boolean isValidEmail(String email) { | |
// email pattern detector - taken from android utils | |
Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile( | |
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + | |
"\\@" + | |
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + | |
"(" + | |
"\\." + | |
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + | |
")+"); | |
if (isNullOrEmpty(email)) { | |
return false; | |
} else { | |
return EMAIL_ADDRESS_PATTERN.matcher(email).matches(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment