/** * Validate streams generate the same output. * * @param expIn Expected input stream. * @param actIn Actual input stream. * @param expSize Expected size of the streams. * @param seek Seek to use async position-based reading or {@code null} to use simple continuous * reading. * @throws IOException In case of any IO exception. */ private void assertEqualStreams( InputStream expIn, GridGgfsInputStream actIn, @Nullable Long expSize, @Nullable Long seek) throws IOException { if (seek != null) expIn.skip(seek); int bufSize = 2345; byte buf1[] = new byte[bufSize]; byte buf2[] = new byte[bufSize]; long pos = 0; long start = System.currentTimeMillis(); while (true) { int read = (int) Math.min(bufSize, expSize - pos); int i1; if (seek == null) i1 = actIn.read(buf1, 0, read); else if (seek % 2 == 0) i1 = actIn.read(pos + seek, buf1, 0, read); else { i1 = read; actIn.readFully(pos + seek, buf1, 0, read); } // Read at least 0 byte, but don't read more then 'i1' or 'read'. int i2 = expIn.read(buf2, 0, Math.max(0, Math.min(i1, read))); if (i1 != i2) { fail( "Expects the same data [read=" + read + ", pos=" + pos + ", seek=" + seek + ", i1=" + i1 + ", i2=" + i2 + ']'); } if (i1 == -1) break; // EOF // i1 == bufSize => compare buffers. // i1 < bufSize => Compare part of buffers, rest of buffers are equal from previous // iteration. assertTrue( "Expects the same data [read=" + read + ", pos=" + pos + ", seek=" + seek + ", i1=" + i1 + ", i2=" + i2 + ']', Arrays.equals(buf1, buf2)); if (read == 0) break; // Nothing more to read. pos += i1; } if (expSize != null) assertEquals(expSize.longValue(), pos); long time = System.currentTimeMillis() - start; if (time != 0 && log.isInfoEnabled()) { log.info( String.format( "Streams were compared in continuous reading " + "[size=%7d, rate=%3.1f MB/sec]", expSize, expSize * 1000. / time / 1024 / 1024)); } }
/** * Checks availability of a classpath resource. * * @param name Resource name. * @return {@code true} if resource is available and ready for read, {@code false} otherwise. */ private boolean resourceAvailable(String name) { InputStream cfgStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); if (cfgStream == null) { log.error("Classpath resource not found: " + name); return false; } try { // Read a single byte to force actual content access by JVM. cfgStream.read(); return true; } catch (IOException e) { log.error("Failed to read classpath resource: " + name, e); return false; } finally { U.close(cfgStream, log); } }