Created
December 19, 2012 22:13
-
-
Save ttim/4341036 to your computer and use it in GitHub Desktop.
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
package ru.abishev; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class SweetHome { | |
public static <T> List<T> union(List<T> l1, List<T> l2) { | |
List<T> result = new ArrayList<T>(); | |
result.addAll(l1); | |
result.addAll(l2); | |
return result; | |
} | |
public static File root() { | |
return new File("."); | |
} | |
public static URL safeUrl(String url) { | |
try { | |
return new URL(url); | |
} catch (MalformedURLException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static String urlContent(URL url) throws IOException { | |
InputStream input = url.openStream(); | |
ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
pump(input, output); | |
input.close(); | |
return output.toString(); | |
} | |
public static String content(File file) { | |
try { | |
FileInputStream input = new FileInputStream(file); | |
ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
pump(input, output); | |
input.close(); | |
return output.toString(); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static List<File> files(File root) { | |
List<File> result = new ArrayList<File>(); | |
collectFiles(root, result); | |
return result; | |
} | |
private static void collectFiles(File file, List<File> result) { | |
if (file.isFile()) { | |
result.add(file); | |
} else { | |
for (File inner : file.listFiles()) { | |
collectFiles(inner, result); | |
} | |
} | |
} | |
public static List<File> files(String... paths) { | |
List<File> result = new ArrayList<File>(); | |
for (String path : paths) { | |
result.add(new File(path)); | |
} | |
return result; | |
} | |
public static List<File> withExtension(String ext, List<File> files) { | |
List<File> result = new ArrayList<File>(); | |
for (File file : files) { | |
if (file.getName().endsWith(ext)) { | |
result.add(file); | |
} | |
} | |
return result; | |
} | |
public static void pump(InputStream input, OutputStream output) throws IOException { | |
// todo: use byte[] Luke | |
int b; | |
while ((b = input.read()) != -1) { | |
output.write(b); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment