1 package org.robolectric.util; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.net.MalformedURLException; 9 import java.net.URL; 10 import java.util.ArrayList; 11 import java.util.List; 12 import java.util.regex.Matcher; 13 import java.util.regex.Pattern; 14 15 /** 16 * Generic collection of utility methods. 17 */ 18 public class Util { 19 copy(InputStream in, OutputStream out)20 public static void copy(InputStream in, OutputStream out) throws IOException { 21 byte[] buffer = new byte[8196]; 22 int len; 23 try { 24 while ((len = in.read(buffer)) != -1) { 25 out.write(buffer, 0, len); 26 } 27 } finally { 28 in.close(); 29 } 30 } 31 32 /** 33 * This method consumes an input stream and returns its content. 34 * 35 * @param is The input stream to read from. 36 * @return The bytes read from the stream. 37 * @throws IOException Error reading from stream. 38 */ readBytes(InputStream is)39 public static byte[] readBytes(InputStream is) throws IOException { 40 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(is.available())) { 41 copy(is, bos); 42 return bos.toByteArray(); 43 } 44 } 45 reverse(T[] array)46 public static <T> T[] reverse(T[] array) { 47 for (int i = 0; i < array.length / 2; i++) { 48 int destI = array.length - i - 1; 49 T o = array[destI]; 50 array[destI] = array[i]; 51 array[i] = o; 52 } 53 return array; 54 } 55 file(String... pathParts)56 public static File file(String... pathParts) { 57 return file(new File("."), pathParts); 58 } 59 file(File f, String... pathParts)60 public static File file(File f, String... pathParts) { 61 for (String pathPart : pathParts) { 62 f = new File(f, pathPart); 63 } 64 65 String dotSlash = "." + File.separator; 66 if (f.getPath().startsWith(dotSlash)) { 67 f = new File(f.getPath().substring(dotSlash.length())); 68 } 69 70 return f; 71 } 72 73 private static final Pattern WINDOWS_UNC_RE = 74 Pattern.compile("^\\\\\\\\(?<host>[^\\\\]+)\\\\(?<path>.*)$"); 75 private static final Pattern WINDOWS_LOCAL_RE = 76 Pattern.compile("^(?<volume>[A-Za-z]:)\\\\(?<path>.*)$"); 77 78 @SuppressWarnings("NewApi") url(String osPath)79 public static URL url(String osPath) throws MalformedURLException { 80 // We should just use Paths.get(path).toUri().toURL() here, but impossible to test Windows' 81 // behavior on Linux (for CI), and this code is going away soon anyway so who cares. 82 83 // Starts with double backslash, is likely a UNC path 84 Matcher windowsUncMatcher = WINDOWS_UNC_RE.matcher(osPath); 85 if (windowsUncMatcher.find()) { 86 String host = windowsUncMatcher.group("host"); 87 String path = windowsUncMatcher.group("path").replace('\\', '/'); 88 return new URL("file://" + host + "/" + path.replace(" ", "%20")); 89 } 90 91 Matcher windowsLocalMatcher = WINDOWS_LOCAL_RE.matcher(osPath); 92 if (windowsLocalMatcher.find()) { 93 String volume = windowsLocalMatcher.group("volume"); 94 String path = windowsLocalMatcher.group("path").replace('\\', '/'); 95 // this doesn't correspend to what M$ says, but, again, who cares. 96 return new URL("file:" + volume + "/" + path.replace(" ", "%20")); 97 } 98 99 return new URL("file:/" + (osPath.startsWith("/") ? "/" + osPath : osPath)); 100 } 101 intArrayToList(int[] ints)102 public static List<Integer> intArrayToList(int[] ints) { 103 List<Integer> youSuckJava = new ArrayList<>(); 104 for (int attr1 : ints) { 105 youSuckJava.add(attr1); 106 } 107 return youSuckJava; 108 } 109 parseInt(String valueFor)110 public static int parseInt(String valueFor) { 111 if (valueFor.startsWith("0x")) { 112 return Integer.parseInt(valueFor.substring(2), 16); 113 } else { 114 return Integer.parseInt(valueFor, 10); 115 } 116 } 117 } 118