Refine search
@Override public String getBytesRead() { return IOUtils.humanReadableByteCount(totalBytesRead); }
/** * Close this executor. */ @Override public void close() { IOUtils.closeQuietly(socketOut); IOUtils.closeQuietly(socketIn); IOUtils.closeQuietly(socket); }
ByteArrayInputStream idStream = new ByteArrayInputStream(id); long totalLength = 0; while (true) { int type = idStream.read(); if (type == -1) { break; int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); totalLength += len; } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); totalLength += IOUtils.readVarLong(idStream); if (level > 0) { IOUtils.readVarLong(idStream); int digestLength = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, digestLength); } else { throw new IOException("Datastore id type " + type + " for blob " + blobId);
public BytesEntry next() { if (count-- > 0) { try { String key = IOUtils.readString(in); byte[] value = IOUtils.readBytes(in); return new BytesEntry(key, value); } catch (IOException e) { throw new RuntimeException("deserialization failed", e); } } throw new NoSuchElementException(); }
@Override public void writeMap(String key, int count, BytesEntryIterator iterator) throws Exception { if (out == null) { throw new IllegalStateException("no OutputStream provided"); } IOUtils.writeVarInt(out, count); while (iterator.hasNext()) { BytesEntry entry = iterator.next(); IOUtils.writeString(out, entry.getKey()); IOUtils.writeBytes(out, entry.getValue()); } }
int blockLen = IOUtils.readFully(in, block, 0, block.length); count++; if (blockLen == 0) { } else if (blockLen < blockSizeMin) { idStream.write(TYPE_DATA); IOUtils.writeVarInt(idStream, blockLen); idStream.write(block, 0, blockLen); totalLength += blockLen; byte[] digest = messageDigest.digest(); idStream.write(TYPE_HASH); IOUtils.writeVarInt(idStream, level); if (level > 0) { IOUtils.writeVarLong(idStream, totalLength); IOUtils.writeVarLong(idStream, blockLen); totalLength += blockLen; IOUtils.writeVarInt(idStream, digest.length); idStream.write(digest); convertBlobToId(new ByteArrayInputStream(idBlock), idStream, level + 1, totalLength); count = 1; convertBlobToId(new ByteArrayInputStream(idBlock), idStream, level + 1, totalLength);
private static void testVarInt(int x, int expectedLen) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.writeVarInt(out, x); byte[] data = out.toByteArray(); assertTrue(data.length <= 5); if (expectedLen > 0) { assertEquals(expectedLen, data.length); } ByteArrayInputStream in = new ByteArrayInputStream(data); int x2 = IOUtils.readVarInt(in); assertEquals(x, x2); assertEquals(-1, in.read()); }
private static void testVarLong(long x, int expectedLen) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.writeVarLong(out, x); byte[] data = out.toByteArray(); assertTrue(data.length <= 10); if (expectedLen > 0) { assertEquals(expectedLen, data.length); } ByteArrayInputStream in = new ByteArrayInputStream(data); long x2 = IOUtils.readVarLong(in); assertEquals(x, x2); assertEquals(-1, in.read()); }
public void testBytesReadWrite() throws IOException { final Random r = new Random(); int iterations = 1000; while (iterations-- > 0) { int n = Math.abs(r.nextInt()) % 0x40000; byte[] buf = new byte[n]; r.nextBytes(buf); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.writeBytes(out, buf); byte[] buf1 = IOUtils.readBytes(new ByteArrayInputStream(out.toByteArray())); assertEquals(buf, buf1); } }
public void testCopyStream() throws IOException { final Random r = new Random(1); for (int length : Lists.newArrayList(0, 1, 1000, 4096, 4097, 1024*1024)) { byte[] inData = new byte[length]; r.nextBytes(inData); ByteArrayInputStream in = new ByteArrayInputStream(inData); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out); assertEquals(inData, out.toByteArray()); } }
public void testVarEOF() throws IOException { try { IOUtils.readVarInt(new ByteArrayInputStream(new byte[0])); fail(); } catch (EOFException e) { // expected } try { IOUtils.readVarLong(new ByteArrayInputStream(new byte[0])); fail(); } catch (EOFException e) { // expected } }
public void testReadFully() throws IOException { final Random r = new Random(1); byte[] data = new byte[1000]; final AtomicInteger readCount = new AtomicInteger(); r.nextBytes(data); FilterInputStream in = new FilterInputStream(new ByteArrayInputStream(data)) { @Override public int read(byte[] buffer, int off, int max) throws IOException { readCount.incrementAndGet(); if (r.nextInt(10) == 0) { return 0; } return in.read(buffer, off, Math.min(10, max)); } }; in.mark(10000); byte[] test = new byte[1000]; // readFully is not supposed to call read when reading 0 bytes assertEquals(0, IOUtils.readFully(in, test, 0, 0)); assertEquals(0, readCount.get()); assertEquals(1000, IOUtils.readFully(in, test, 0, 1000)); IOUtilsTest.assertEquals(data, test); test = new byte[1001]; in.reset(); in.mark(10000); assertEquals(1000, IOUtils.readFully(in, test, 0, 1001)); assertEquals(0, IOUtils.readFully(in, test, 0, 0)); }
public void testVarInt() throws IOException { testVarInt(0, 1); testVarInt(0x7f, 1); testVarInt(0x80, 2); testVarInt(0x3fff, 2); testVarInt(0x4000, 3); testVarInt(0x1fffff, 3); testVarInt(0x200000, 4); testVarInt(0xfffffff, 4); testVarInt(0x10000000, 5); testVarInt(-1, 5); for (int x = 0; x < 0x20000; x++) { testVarInt(x, 0); testVarInt(Integer.MIN_VALUE + x, 0); testVarInt(Integer.MAX_VALUE - x, 5); testVarInt(0x200000 + x - 100, 0); testVarInt(0x10000000 + x - 100, 0); } Random r = new Random(1); for (int i = 0; i < 100000; i++) { testVarInt(r.nextInt(), 0); testVarInt(r.nextInt(10000000), 0); } // trailing 0s are never written, but are an alternative way to encode a value InputStream in = new ByteArrayInputStream(new byte[]{(byte) 0x80, 0}); assertEquals(0, IOUtils.readVarInt(in)); assertEquals(-1, in.read()); }
InputStream in = new ByteArrayInputStream(new byte[]{(byte) 0x80, 0}); assertEquals(0, IOUtils.readVarLong(in)); assertEquals(-1, in.read());
@Override public void service(Request request, Response response) throws IOException { String file = request.getFile(); if (file.endsWith("/")) { file += "index.html"; } InputStream in = FileServlet.class.getResourceAsStream(file.substring(1)); if (in != null) { try { int dotIndex = file.lastIndexOf('.'); if (dotIndex != -1) { String contentType = MIME_TYPES.get(file.substring(dotIndex + 1)); if (contentType == null) { contentType = "application/octet-stream"; } response.setContentType(contentType); } IOUtils.copy(in, response.getOutputStream()); } finally { IOUtils.closeQuietly(in); } } else { response.setStatusCode(404); } }
IOUtils.writeVarInt(idStream, 0); IOUtils.writeVarLong(idStream, length); byte[] digest = messageDigest.digest(); File f = getFile(digest, false); IOUtils.writeVarInt(idStream, digest.length); idStream.write(digest); byte[] id = idStream.toByteArray();
private static void readFully(InputStream in, byte[] b, int off, int len) throws IOException { int count = IOUtils.readFully(in, b, off, len); if (count < len) { String msg = String.format("Expected %d bytes, actually received: %d", len, count); throw new EOFException(msg); } }
private void copy(String resource, String dir) throws IOException { String fileName = dir + resource.substring(resource.lastIndexOf("/")); File outputFile = new File(fileName); if (outputFile.createNewFile()) { InputStream inputStream = null; FileOutputStream outputStream = null; try { inputStream = getClass().getResourceAsStream(resource); outputStream = new FileOutputStream(outputFile); IOUtils.copy(inputStream, outputStream); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { // do nothing } } if (outputStream != null) { try { outputStream.close(); } catch (Exception e) { // do nothing } } } } }
@Override public void write(String key, byte[] value) throws Exception { if (out == null) { throw new IllegalStateException("no OutputStream provided"); } IOUtils.writeBytes(out, value); }
@Override public int readIntValue(String key) throws Exception { if (in == null) { throw new IllegalStateException("no InputStream provided"); } return IOUtils.readVarInt(in); }