org.apache.commons.io.IOUtils.readFully()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(13.8k)|赞(0)|评价(0)|浏览(766)

本文整理了Java中org.apache.commons.io.IOUtils.readFully()方法的一些代码示例,展示了IOUtils.readFully()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.readFully()方法的具体详情如下:
包路径:org.apache.commons.io.IOUtils
类名称:IOUtils
方法名:readFully

IOUtils.readFully介绍

[英]Reads the requested number of bytes or fail if there are not enough left.

This allows for the possibility that InputStream#read(byte[],int,int) may not read as many bytes as requested (most likely because of reaching EOF).
[中]读取请求的字节数,如果剩余字节数不足,则读取失败。
这使得InputStream#read(byte[],int,int)可能无法读取请求的字节数(很可能是因为达到EOF)。

代码示例

代码示例来源:origin: commons-io/commons-io

/**
 * Reads the requested number of characters or fail if there are not enough left.
 * <p>
 * This allows for the possibility that {@link Reader#read(char[], int, int)} may
 * not read as many characters as requested (most likely because of reaching EOF).
 *
 * @param input where to read input from
 * @param buffer destination
 * @throws IOException              if there is a problem reading the file
 * @throws IllegalArgumentException if length is negative
 * @throws EOFException             if the number of characters read was incorrect
 * @since 2.2
 */
public static void readFully(final Reader input, final char[] buffer) throws IOException {
  readFully(input, buffer, 0, buffer.length);
}

代码示例来源:origin: commons-io/commons-io

/**
 * Reads the requested number of bytes or fail if there are not enough left.
 * <p>
 * This allows for the possibility that {@link InputStream#read(byte[], int, int)} may
 * not read as many bytes as requested (most likely because of reaching EOF).
 *
 * @param input where to read input from
 * @param buffer destination
 * @throws IOException              if there is a problem reading the file
 * @throws IllegalArgumentException if length is negative
 * @throws EOFException             if the number of bytes read was incorrect
 * @since 2.2
 */
public static void readFully(final InputStream input, final byte[] buffer) throws IOException {
  readFully(input, buffer, 0, buffer.length);
}

代码示例来源:origin: commons-io/commons-io

/**
 * Reads the requested number of bytes or fail if there are not enough left.
 * <p>
 * This allows for the possibility that {@link InputStream#read(byte[], int, int)} may
 * not read as many bytes as requested (most likely because of reaching EOF).
 *
 * @param input where to read input from
 * @param length length to read, must be &gt;= 0
 * @return the bytes read from input
 * @throws IOException              if there is a problem reading the file
 * @throws IllegalArgumentException if length is negative
 * @throws EOFException             if the number of bytes read was incorrect
 * @since 2.5
 */
public static byte[] readFully(final InputStream input, final int length) throws IOException {
  final byte[] buffer = new byte[length];
  readFully(input, buffer, 0, buffer.length);
  return buffer;
}

代码示例来源:origin: apache/hbase

/**
  * @return Byte array read from the stream.
  * @throws IOException
  */
 private byte [] readByteArray(final InputStream in) throws IOException {
  byte [] intArray = new byte[Bytes.SIZEOF_INT];
  IOUtils.readFully(in, intArray);
  int length = Bytes.toInt(intArray);
  byte [] bytes = new byte [length];
  IOUtils.readFully(in, bytes);
  return bytes;
 }
}

代码示例来源:origin: apache/hbase

/**
  * @return Byte array read from the stream.
  * @throws IOException
  */
 private byte[] readByteArray(final InputStream in) throws IOException {
  byte[] intArray = new byte[Bytes.SIZEOF_INT];
  IOUtils.readFully(in, intArray);
  int length = Bytes.toInt(intArray);
  byte[] bytes = new byte[length];
  IOUtils.readFully(in, bytes);
  return bytes;
 }
}

代码示例来源:origin: apache/incubator-gobblin

private Integer readKey()
  throws IOException {
 IOUtils.readFully(origStream, buffer, 0, 4);
 try {
  return Integer.valueOf(new String(buffer, 0, 4, StandardCharsets.UTF_8));
 } catch (NumberFormatException e) {
  throw new IOException("Expected to be able to parse first 4 bytes of stream as an ASCII keyId");
 }
}

代码示例来源:origin: apache/hbase

/**
 * Decrypt a block of ciphertext read in from a stream with the given
 * cipher and context
 * <p>
 * The decryptor's state will be finalized. It should be reinitialized or
 * returned to the pool.
 * @param dest
 * @param destOffset
 * @param in
 * @param destSize
 * @param d
 * @throws IOException
 */
public static void decrypt(byte[] dest, int destOffset, InputStream in,
  int destSize, Decryptor d) throws IOException {
 InputStream cin = d.createDecryptionStream(in);
 try {
  IOUtils.readFully(cin, dest, destOffset, destSize);
 } finally {
  cin.close();
 }
}

代码示例来源:origin: commons-io/commons-io

@Test public void testReadFully_InputStream_Offset() throws Exception {
  final byte[] bytes = "abcd1234".getBytes("UTF-8");
  final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
  final byte[] buffer = "wx00000000".getBytes("UTF-8");
  IOUtils.readFully(stream, buffer, 2, 8);
  assertEquals("wxabcd1234", new String(buffer, 0, buffer.length, "UTF-8"));
  IOUtils.closeQuietly(stream);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testReadFully_Reader() throws Exception {
  final int size = 1027;
  final char[] buffer = new char[size];
  final Reader input = new CharArrayReader(new char[size]);
  IOUtils.readFully(input, buffer, 0, 0);
  IOUtils.readFully(input, buffer, 0, size - 3);
  try {
    IOUtils.readFully(input, buffer, 0, -1);
    fail("Should have failed with IllegalArgumentException");
  } catch (final IllegalArgumentException expected) {
    // expected
  }
  try {
    IOUtils.readFully(input, buffer, 0, 5);
    fail("Should have failed with EOFException");
  } catch (final EOFException expected) {
    // expected
  }
  IOUtils.closeQuietly(input);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testReadFully_InputStream_ByteArray() throws Exception {
  final int size = 1027;
  final byte[] buffer = new byte[size];
  final InputStream input = new ByteArrayInputStream(new byte[size]);
  try {
    IOUtils.readFully(input, buffer, 0, -1);
    fail("Should have failed with IllegalArgumentException");
  } catch (final IllegalArgumentException expected) {
    // expected
  }
  IOUtils.readFully(input, buffer, 0, 0);
  IOUtils.readFully(input, buffer, 0, size - 1);
  try {
    IOUtils.readFully(input, buffer, 0, 2);
    fail("Should have failed with EOFxception");
  } catch (final EOFException expected) {
    // expected
  }
  IOUtils.closeQuietly(input);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testReadFully_Reader_Offset() throws Exception {
  final Reader reader = new StringReader("abcd1234");
  final char[] buffer = "wx00000000".toCharArray();
  IOUtils.readFully(reader, buffer, 2, 8);
  assertEquals("wxabcd1234", new String(buffer));
  IOUtils.closeQuietly(reader);
}

代码示例来源:origin: commons-io/commons-io

@Test public void testReadFully_InputStream__ReturnByteArray() throws Exception {
  final byte[] bytes = "abcd1234".getBytes("UTF-8");
  final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
  final byte[] result = IOUtils.readFully(stream, bytes.length);
  IOUtils.closeQuietly(stream);
  assertEqualContent(result, bytes);
}

代码示例来源:origin: apache/storm

private Set<String> getTopologyDependencyKeys(Set<String> activeTopologyCodeKeys) {
  Set<String> activeTopologyDependencies = new TreeSet<>();
  Subject subject = ReqContext.context().subject();
  for (String activeTopologyCodeKey : activeTopologyCodeKeys) {
    try (InputStreamWithMeta blob = blobStore.getBlob(activeTopologyCodeKey, subject)) {
      byte[] blobContent = IOUtils.readFully(blob, new Long(blob.getFileLength()).intValue());
      StormTopology stormCode = Utils.deserialize(blobContent, StormTopology.class);
      if (stormCode.is_set_dependency_jars()) {
        activeTopologyDependencies.addAll(stormCode.get_dependency_jars());
      }
      if (stormCode.is_set_dependency_artifacts()) {
        activeTopologyDependencies.addAll(stormCode.get_dependency_artifacts());
      }
    } catch (AuthorizationException | KeyNotFoundException | IOException e) {
      LOG.error("Exception occurs while reading blob for key: "
           + activeTopologyCodeKey
           + ", exception: "
           + e, e);
      throw new RuntimeException("Exception occurs while reading blob for key: "
                    + activeTopologyCodeKey
                    + ", exception: " + e, e);
    }
  }
  return activeTopologyDependencies;
}

代码示例来源:origin: commons-io/commons-io

@Test public void testReadFully_ReadableByteChannel() throws Exception {
  final ByteBuffer buffer = ByteBuffer.allocate(FILE_SIZE);
  final FileInputStream fileInputStream = new FileInputStream(m_testFile);
  final FileChannel input = fileInputStream.getChannel();
  try {
    IOUtils.readFully(input, buffer);
    assertEquals(FILE_SIZE, buffer.position());
    assertEquals(0, buffer.remaining());
    assertEquals(0, input.read(buffer));
    IOUtils.readFully(input, buffer);
    assertEquals(FILE_SIZE, buffer.position());
    assertEquals(0, buffer.remaining());
    assertEquals(0, input.read(buffer));
    IOUtils.readFully(input, buffer);
    buffer.clear();
    try {
      IOUtils.readFully(input, buffer);
      fail("Should have failed with EOFxception");
    } catch (final EOFException expected) {
      // expected
    }
  } finally {
    IOUtils.closeQuietly(input, fileInputStream);
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Override
  protected Class<?> findClass(final String name) throws ClassNotFoundException {
    final String path = name.replace('.', '/').concat(".class");
    final URL resource = super.getResource(path);
    if (resource == null) {
      throw new ClassNotFoundException(name);
    }
    try {
      final URLConnection uc = resource.openConnection();
      final int len = uc.getContentLength();
      final InputStream in = new BufferedInputStream(uc.getInputStream());
      final byte[] bytecode = new byte[len];
      try {
        IOUtils.readFully(in, bytecode);
      } finally {
        Closer.closeSilently(in);
      }
      return defineClass(name, bytecode, 0, bytecode.length);
    } catch (final IOException e) {
      Throwables.rethrow(e);
      return null; // unreachable
    }
  }
}

代码示例来源:origin: commons-io/commons-io

@Test public void testRead_ReadableByteChannel() throws Exception {
  final ByteBuffer buffer = ByteBuffer.allocate(FILE_SIZE);
  final FileInputStream fileInputStream = new FileInputStream(m_testFile);
  final FileChannel input = fileInputStream.getChannel();
  try {
    assertEquals(FILE_SIZE, IOUtils.read(input, buffer));
    assertEquals(0, IOUtils.read(input, buffer));
    assertEquals(0, buffer.remaining());
    assertEquals(0, input.read(buffer));
    buffer.clear();
    try {
      IOUtils.readFully(input, buffer);
      fail("Should have failed with EOFxception");
    } catch (final EOFException expected) {
      // expected
    }
  } finally {
    IOUtils.closeQuietly(input, fileInputStream);
  }}

代码示例来源:origin: apache/hbase

@Override
protected Cell parseCell() throws IOException {
 byte [] row = readByteArray(this.in);
 byte [] family = readByteArray(in);
 byte [] qualifier = readByteArray(in);
 byte [] longArray = new byte[Bytes.SIZEOF_LONG];
 IOUtils.readFully(this.in, longArray);
 long timestamp = Bytes.toLong(longArray);
 byte type = (byte) this.in.read();
 byte[] value = readByteArray(in);
 // Read memstore version
 byte[] memstoreTSArray = new byte[Bytes.SIZEOF_LONG];
 IOUtils.readFully(this.in, memstoreTSArray);
 long memstoreTS = Bytes.toLong(memstoreTSArray);
 return cellBuilder.clear()
     .setRow(row)
     .setFamily(family)
     .setQualifier(qualifier)
     .setTimestamp(timestamp)
     .setType(type)
     .setValue(value)
     .setSequenceId(memstoreTS)
     .build();
}

代码示例来源:origin: apache/hbase

@Override
protected Cell parseCell() throws IOException {
 byte[] row = readByteArray(this.in);
 byte[] family = readByteArray(in);
 byte[] qualifier = readByteArray(in);
 byte[] longArray = new byte[Bytes.SIZEOF_LONG];
 IOUtils.readFully(this.in, longArray);
 long timestamp = Bytes.toLong(longArray);
 byte type = (byte) this.in.read();
 byte[] value = readByteArray(in);
 byte[] tags = readByteArray(in);
 // Read memstore version
 byte[] memstoreTSArray = new byte[Bytes.SIZEOF_LONG];
 IOUtils.readFully(this.in, memstoreTSArray);
 long memstoreTS = Bytes.toLong(memstoreTSArray);
 return cellBuilder.clear()
     .setRow(row)
     .setFamily(family)
     .setQualifier(qualifier)
     .setTimestamp(timestamp)
     .setType(type)
     .setValue(value)
     .setSequenceId(memstoreTS)
     .setTags(tags)
     .build();
}

代码示例来源:origin: apache/hbase

@Test
public void testAESAlgorithm() throws Exception {
 Configuration conf = HBaseConfiguration.create();
 Cipher aes = Encryption.getCipher(conf, "AES");
 assertEquals(CommonsCryptoAES.KEY_LENGTH, aes.getKeyLength());
 assertEquals(CommonsCryptoAES.IV_LENGTH, aes.getIvLength());
 Encryptor e = aes.getEncryptor();
 e.setKey(new SecretKeySpec(Bytes.fromHex("2b7e151628aed2a6abf7158809cf4f3c"), "AES"));
 e.setIv(Bytes.fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"));
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 OutputStream cout = e.createEncryptionStream(out);
 cout.write(Bytes.fromHex("6bc1bee22e409f96e93d7e117393172a"));
 cout.write(Bytes.fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
 cout.write(Bytes.fromHex("30c81c46a35ce411e5fbc1191a0a52ef"));
 cout.write(Bytes.fromHex("f69f2445df4f9b17ad2b417be66c3710"));
 cout.close();
 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
 byte[] b = new byte[16];
 IOUtils.readFully(in, b);
 assertTrue("Failed #1", Bytes.equals(b, Bytes.fromHex("874d6191b620e3261bef6864990db6ce")));
 IOUtils.readFully(in, b);
 assertTrue("Failed #2", Bytes.equals(b, Bytes.fromHex("9806f66b7970fdff8617187bb9fffdff")));
 IOUtils.readFully(in, b);
 assertTrue("Failed #3", Bytes.equals(b, Bytes.fromHex("5ae4df3edbd5d35e5b4f09020db03eab")));
 IOUtils.readFully(in, b);
 assertTrue("Failed #4", Bytes.equals(b, Bytes.fromHex("1e031dda2fbe03d1792170a0f3009cee")));
}

代码示例来源:origin: apache/hbase

@Test
public void testAESAlgorithm() throws Exception {
 Configuration conf = HBaseConfiguration.create();
 Cipher aes = Encryption.getCipher(conf, "AES");
 assertEquals(AES.KEY_LENGTH, aes.getKeyLength());
 assertEquals(AES.IV_LENGTH, aes.getIvLength());
 Encryptor e = aes.getEncryptor();
 e.setKey(new SecretKeySpec(Bytes.fromHex("2b7e151628aed2a6abf7158809cf4f3c"), "AES"));
 e.setIv(Bytes.fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"));
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 OutputStream cout = e.createEncryptionStream(out);
 cout.write(Bytes.fromHex("6bc1bee22e409f96e93d7e117393172a"));
 cout.write(Bytes.fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
 cout.write(Bytes.fromHex("30c81c46a35ce411e5fbc1191a0a52ef"));
 cout.write(Bytes.fromHex("f69f2445df4f9b17ad2b417be66c3710"));
 cout.close();
 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
 byte[] b = new byte[16];
 IOUtils.readFully(in, b);
 assertTrue("Failed #1", Bytes.equals(b, Bytes.fromHex("874d6191b620e3261bef6864990db6ce")));
 IOUtils.readFully(in, b);
 assertTrue("Failed #2", Bytes.equals(b, Bytes.fromHex("9806f66b7970fdff8617187bb9fffdff")));
 IOUtils.readFully(in, b);
 assertTrue("Failed #3", Bytes.equals(b, Bytes.fromHex("5ae4df3edbd5d35e5b4f09020db03eab")));
 IOUtils.readFully(in, b);
 assertTrue("Failed #4", Bytes.equals(b, Bytes.fromHex("1e031dda2fbe03d1792170a0f3009cee")));
}

相关文章