/** * Returns true if the given files exist, are not directories, and contain the same bytes. * * @throws IOException if an I/O error occurs */ public static boolean equal(File file1, File file2) throws IOException { checkNotNull(file1); checkNotNull(file2); if (file1 == file2 || file1.equals(file2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ long len1 = file1.length(); long len2 = file2.length(); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return asByteSource(file1).contentEquals(asByteSource(file2)); }
public void testContentEquals() throws IOException { assertTrue( source.contentEquals( new ByteSource() { @Override public InputStream openStream() throws IOException { return new RandomAmountInputStream( new ByteArrayInputStream(expected), new Random()); } })); }
/** * Returns true if the given files exist, are not directories, and contain the same bytes. * * @throws IOException if an I/O error occurs */ public static boolean equal(File file1, File file2) throws IOException { checkNotNull(file1); checkNotNull(file2); if (file1 == file2 || file1.equals(file2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ long len1 = file1.length(); long len2 = file2.length(); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return asByteSource(file1).contentEquals(asByteSource(file2)); }
/** * Returns true if the files located by the given paths exist, are not directories, and contain * the same bytes. * * @throws IOException if an I/O error occurs * @since 22.0 */ public static boolean equal(Path path1, Path path2) throws IOException { checkNotNull(path1); checkNotNull(path2); if (Files.isSameFile(path1, path2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ ByteSource source1 = asByteSource(path1); ByteSource source2 = asByteSource(path2); long len1 = source1.sizeIfKnown().or(0L); long len2 = source2.sizeIfKnown().or(0L); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return source1.contentEquals(source2); }
/** * Returns true if the given files exist, are not directories, and contain the same bytes. * * @throws IOException if an I/O error occurs */ public static boolean equal(File file1, File file2) throws IOException { checkNotNull(file1); checkNotNull(file2); if (file1 == file2 || file1.equals(file2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ long len1 = file1.length(); long len2 = file2.length(); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return asByteSource(file1).contentEquals(asByteSource(file2)); }
private static boolean contentEquals(URL left, URL right) throws IOException { return Resources.asByteSource(left).contentEquals(Resources.asByteSource(right)); }
/** * Returns true if the files located by the given paths exist, are not directories, and contain * the same bytes. * * @throws IOException if an I/O error occurs * @since 22.0 */ public static boolean equal(Path path1, Path path2) throws IOException { checkNotNull(path1); checkNotNull(path2); if (Files.isSameFile(path1, path2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ ByteSource source1 = asByteSource(path1); ByteSource source2 = asByteSource(path2); long len1 = source1.sizeIfKnown().or(0L); long len2 = source2.sizeIfKnown().or(0L); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return source1.contentEquals(source2); }
private void joinHelper(Integer... spans) throws Exception { List<ByteSource> sources = Lists.newArrayList(); int start = 0; for (Integer span : spans) { sources.add(newByteSource(start, span)); start += span; } ByteSource joined = ByteSource.concat(sources); assertTrue(newByteSource(0, start).contentEquals(joined)); }
/** * Returns true if the files located by the given paths exist, are not directories, and contain * the same bytes. * * @throws IOException if an I/O error occurs * @since 22.0 */ public static boolean equal(Path path1, Path path2) throws IOException { checkNotNull(path1); checkNotNull(path2); if (Files.isSameFile(path1, path2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ ByteSource source1 = asByteSource(path1); ByteSource source2 = asByteSource(path2); long len1 = source1.sizeIfKnown().or(0L); long len2 = source2.sizeIfKnown().or(0L); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return source1.contentEquals(source2); }
public void testSlice_constrainedRange() throws IOException { long size = source.read().length; if (size >= 2) { ByteSource sliced = source.slice(1, size - 2); assertEquals(size - 2, sliced.read().length); ByteSource resliced = sliced.slice(0, size - 1); assertTrue(sliced.contentEquals(resliced)); } }
public void testEqual() throws IOException { try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { Path fooPath = fs.getPath("foo"); Path barPath = fs.getPath("bar"); MoreFiles.asCharSink(fooPath, UTF_8).write("foo"); MoreFiles.asCharSink(barPath, UTF_8).write("barbar"); assertThat(MoreFiles.equal(fooPath, barPath)).isFalse(); assertThat(MoreFiles.equal(fooPath, fooPath)).isTrue(); assertThat(MoreFiles.asByteSource(fooPath).contentEquals(MoreFiles.asByteSource(fooPath))) .isTrue(); Path fooCopy = Files.copy(fooPath, fs.getPath("fooCopy")); assertThat(Files.isSameFile(fooPath, fooCopy)).isFalse(); assertThat(MoreFiles.equal(fooPath, fooCopy)).isTrue(); MoreFiles.asCharSink(fooCopy, UTF_8).write("boo"); assertThat(MoreFiles.asByteSource(fooPath).size()) .isEqualTo(MoreFiles.asByteSource(fooCopy).size()); assertThat(MoreFiles.equal(fooPath, fooCopy)).isFalse(); // should also assert that a Path that erroneously reports a size 0 can still be compared, // not sure how to do that with the Path API } }
public void testEqual() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertFalse(Files.equal(asciiFile, i18nFile)); assertTrue(Files.equal(asciiFile, asciiFile)); File temp = createTempFile(); Files.copy(asciiFile, temp); assertTrue(Files.equal(asciiFile, temp)); Files.copy(i18nFile, temp); assertTrue(Files.equal(i18nFile, temp)); Files.copy(asciiFile, temp); RandomAccessFile rf = new RandomAccessFile(temp, "rw"); rf.writeByte(0); rf.close(); assertEquals(asciiFile.length(), temp.length()); assertFalse(Files.equal(asciiFile, temp)); assertTrue(Files.asByteSource(asciiFile).contentEquals(Files.asByteSource(asciiFile))); // 0-length files have special treatment (/proc, etc.) assertTrue(Files.equal(asciiFile, new BadLengthFile(asciiFile, 0))); }
assertTrue(ByteSource.wrap(data).slice(0, chunk1).contentEquals(source));
public boolean contentEquals(NonIoByteSource other) { try { return delegate.contentEquals(other.delegate); } catch (IOException ex) { throw new AssertionError("An illegal IOException was throw"); } }
public boolean contentEquals(NonIoByteSource other) { try { return delegate.contentEquals(other.delegate); } catch (IOException ex) { throw new AssertionError("An illegal IOException was throw"); } }
public boolean contentEquals(NonIoByteSource other) { try { return delegate.contentEquals(other.delegate); } catch (IOException ex) { throw new AssertionError("An illegal IOException was throw"); } }
ArgumentCaptor<ByteSource> byteSourceCaptor = ArgumentCaptor.forClass(ByteSource.class); ByteSource byteSource = mock(ByteSource.class); when(byteSource.contentEquals(byteSourceCaptor.capture())).thenReturn(true);
/** Asserts that the actual file's contents are equal to {@code expected}. */ public void hasContents(ByteSource expected) { try { if (!asByteSource(actual()).contentEquals(expected)) { fail("has contents", expected); } } catch (IOException e) { throw new RuntimeException(e); } }
/** Asserts that the actual file's contents are equal to {@code expected}. */ public void hasContents(ByteSource expected) { try { if (!asByteSource(actual()).contentEquals(expected)) { fail("has contents", expected); } } catch (IOException e) { throw new RuntimeException(e); } }
public void test_toCharSource_noBomUtf8() throws IOException { byte[] bytes = {'H', 'e', 'l', 'l', 'o'}; ByteSource byteSource = ByteSource.wrap(bytes); CharSource charSource = UnicodeBom.toCharSource(byteSource); String str = charSource.read(); assertEquals(str, "Hello"); assertTrue(charSource.asByteSource(StandardCharsets.UTF_8).contentEquals(byteSource)); assertEquals(charSource.toString().startsWith("UnicodeBom"), true); }