public void testToByteArray() throws IOException {
File asciiFile = getTestFile("ascii.txt");
File i18nFile = getTestFile("i18n.txt");
assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII),
Files.toByteArray(asciiFile)));
assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8),
Files.toByteArray(i18nFile)));
assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8),
Files.asByteSource(i18nFile).read()));
}
public void file_to_byte_array_guava () throws IOException, URISyntaxException {
File file = new File(fileLocation);
byte[] fileInBytes = Files.toByteArray(file);
assertEquals(18, fileInBytes.length);
}
public void createByteSourceFromFileTest() throws Exception {
File f1 = new File("src/main/resources/sample.pdf");
byteSource = Files.asByteSource(f1);
byte[] readBytes = byteSource.read();
assertThat(readBytes,is(Files.toByteArray(f1)));
}
public void testCreateFileByteSink() throws Exception {
File dest = new File("src/test/resources/byteSink.pdf");
dest.deleteOnExit();
byteSink = Files.asByteSink(dest);
File file = new File("src/main/resources/sample.pdf");
byteSink.write(Files.toByteArray(file));
assertThat(Files.toByteArray(dest),is(Files.toByteArray(file)));
}
public static byte[] getFileRequestContent(String fieldName, String filename, boolean finalize) throws IOException {
String content = "--" + CONTENT_BOUNDARY + "\r\n" + "Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + filename + "\"\r\n"+
"Content-Type: application/octet-stream;\r\n\r\n";
File f = new File(filename);
byte[] bytes = Files.toByteArray(f);
ByteArrayOutputStream stream = new ByteArrayOutputStream2();
stream.write(content.getBytes());
stream.write(bytes);
String finish = "\r\n";
if (finalize)
finish += "--" + CONTENT_BOUNDARY + "--\r\n\r\n";
stream.write(finish.getBytes());
stream.flush();
return stream.toByteArray();
}
public static void corruptFile(File file,
byte[] stringToCorrupt,
byte[] replacement) throws IOException {
Preconditions.checkArgument(replacement.length == stringToCorrupt.length);
if (!file.isFile()) {
throw new IllegalArgumentException(
"Given argument is not a file:" + file);
}
byte[] data = Files.toByteArray(file);
int index = Bytes.indexOf(data, stringToCorrupt);
if (index == -1) {
throw new IOException(
"File " + file + " does not contain string " +
new String(stringToCorrupt));
}
for (int i = 0; i < stringToCorrupt.length; i++) {
data[index + i] = replacement[i];
}
Files.write(data, file);
}
public void processChange(final InputFileDetails input, final RecompilationSpec spec) {
// Do not process
if (input.isRemoved()) {
return;
}
final ClassReader classReader;
try {
classReader = new Java9ClassReader(Files.toByteArray(input.getFile()));
} catch (IOException e) {
throw new IllegalArgumentException(String.format("Unable to read class file: '%s'", input.getFile()));
}
String className = classReader.getClassName().replaceAll("/", ".");
DependentsSet actualDependents = previousCompilation.getDependents(className);
if (actualDependents.isDependencyToAll()) {
spec.setFullRebuildCause(actualDependents.getDescription(), input.getFile());
} else {
spec.getClassNames().addAll(actualDependents.getDependentClasses());
}
}
private void remapClasses(File scriptCacheDir, File relocalizedDir, RemappingScriptSource source) {
ScriptSource origin = source.getSource();
String className = origin.getClassName();
if (!relocalizedDir.exists()) {
relocalizedDir.mkdir();
}
File[] files = scriptCacheDir.listFiles();
if (files != null) {
for (File file : files) {
String renamed = file.getName();
if (renamed.startsWith(RemappingScriptSource.MAPPED_SCRIPT)) {
renamed = className + renamed.substring(RemappingScriptSource.MAPPED_SCRIPT.length());
}
ClassWriter cv = new ClassWriter(0);
BuildScriptRemapper remapper = new BuildScriptRemapper(cv, origin);
try {
ClassReader cr = new ClassReader(Files.toByteArray(file));
cr.accept(remapper, 0);
Files.write(cv.toByteArray(), new File(relocalizedDir, renamed));
} catch (IOException ex) {
throw UncheckedException.throwAsUncheckedException(ex);
}
}
}
}
private static Set<String> getComponentTypes(List<String> files, File assetsDir)
throws IOException, JSONException {
Map<String, String> nameTypeMap = createNameTypeMap(assetsDir);
Set<String> componentTypes = Sets.newHashSet();
for (String f : files) {
if (f.endsWith(".scm")) {
File scmFile = new File(f);
String scmContent = new String(Files.toByteArray(scmFile),
PathUtil.DEFAULT_CHARSET);
for (String compName : getTypesFromScm(scmContent)) {
componentTypes.add(nameTypeMap.get(compName));
}
}
}
return componentTypes;
}
private boolean hasContent(byte[] generatedContent, File file) {
if (generatedContent.length != file.length()) {
return false;
}
byte[] existingContent;
try {
existingContent = Files.toByteArray(this.file);
} catch (IOException e) {
// Assume changed if reading old file fails
return false;
}
return Arrays.equals(generatedContent, existingContent);
}