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

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

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

IOUtils.read介绍

[英]Read bytes from an input stream. This implementation guarantees that it will read as many bytes as possible before giving up; this may not always be the case for subclasses of InputStream.
[中]从输入流读取字节。这种实现保证了它在放弃之前将读取尽可能多的字节;InputStream的子类可能并不总是这样。

代码示例

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

/**
 * Reads bytes from an input stream.
 * This implementation guarantees that it will read as many bytes
 * as possible before giving up; this may not always be the case for
 * subclasses of {@link InputStream}.
 *
 * @param input where to read input from
 * @param buffer destination
 * @return actual length read; may be less than requested if EOF was reached
 * @throws IOException if a read error occurs
 * @since 2.2
 */
public static int read(final InputStream input, final byte[] buffer) throws IOException {
  return read(input, buffer, 0, buffer.length);
}

代码示例来源: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: commons-io/commons-io

/**
 * Reads characters from an input character stream.
 * This implementation guarantees that it will read as many characters
 * as possible before giving up; this may not always be the case for
 * subclasses of {@link Reader}.
 *
 * @param input where to read input from
 * @param buffer destination
 * @return actual length read; may be less than requested if EOF was reached
 * @throws IOException if a read error occurs
 * @since 2.2
 */
public static int read(final Reader input, final char[] buffer) throws IOException {
  return read(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
 * @param offset initial offset into buffer
 * @param length length to read, must be &gt;= 0
 * @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, final int offset, final int length)
    throws IOException {
  final int actual = read(input, buffer, offset, length);
  if (actual != length) {
    throw new EOFException("Length to read: " + length + " actual: " + actual);
  }
}

代码示例来源: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
 * @param offset initial offset into buffer
 * @param length length to read, must be &gt;= 0
 * @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, final int offset, final int length)
    throws IOException {
  final int actual = read(input, buffer, offset, length);
  if (actual != length) {
    throw new EOFException("Length to read: " + length + " actual: " + actual);
  }
}

代码示例来源: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 ReadableByteChannel#read(ByteBuffer)} may
 * not read as many bytes as requested (most likely because of reaching EOF).
 *
 * @param input the byte channel to read
 * @param buffer byte buffer destination
 * @throws IOException  if there is a problem reading the file
 * @throws EOFException if the number of bytes read was incorrect
 * @since 2.5
 */
public static void readFully(final ReadableByteChannel input, final ByteBuffer buffer) throws IOException {
  final int expected = buffer.remaining();
  final int actual = read(input, buffer);
  if (actual != expected) {
    throw new EOFException("Length to read: " + expected + " actual: " + actual);
  }
}

代码示例来源:origin: SonarSource/sonarqube

private byte[] readBuffer() throws IOException {
 stream = new BufferedInputStream(Files.newInputStream(filePath), BYTES_TO_DECODE * 2);
 stream.mark(BYTES_TO_DECODE);
 byte[] buf = new byte[BYTES_TO_DECODE];
 int read = IOUtils.read(stream, buf, 0, BYTES_TO_DECODE);
 stream.reset();
 stream.mark(-1);
 return Arrays.copyOf(buf, read);
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Override
 public void handle(String pathInContext, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
  String redirectHeader = httpRequest.getHeader("X-REDIRECT");
  if (redirectHeader != null && !redirectAlreadyPerformed) {
   redirectAlreadyPerformed = true;
   httpResponse.setStatus(Integer.valueOf(redirectHeader));
   httpResponse.setContentLength(0);
   httpResponse.setHeader(LOCATION.toString(), getTargetUrl());
  } else {
   receivedContentType = request.getContentType();
   httpResponse.setStatus(200);
   int len = request.getContentLength();
   httpResponse.setContentLength(len);
   if (len > 0) {
    byte[] buffer = new byte[len];
    IOUtils.read(request.getInputStream(), buffer);
    httpResponse.getOutputStream().write(buffer);
   }
  }
  httpResponse.getOutputStream().flush();
  httpResponse.getOutputStream().close();
 }
};

代码示例来源:origin: Netflix/hollow

boolean fill(Reader reader) throws IOException {
  dataLength = IOUtils.read(reader, data);
  return dataLength < data.length;
}

代码示例来源:origin: trekawek/coffee-gb

private void loadRam(int[] ram, InputStream is, long length) throws IOException {
  byte[] buffer = new byte[ram.length];
  IOUtils.read(is, buffer, 0, Math.min((int) length, ram.length));
  for (int i = 0; i < ram.length; i++) {
    ram[i] = buffer[i] & 0xff;
  }
}

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

boolean read(InputStream is) throws IOException {
  bytesReadLast = IOUtils.read(is, bytes);
  if (DBFReader.STRICT && bytesReadLast != bytes.length) {
    throw new IOException("Truncated record, only read "+bytesReadLast+
        " bytes, but should have read: "+bytes.length);
  }
  return bytesReadLast > 0;
}

代码示例来源:origin: trekawek/coffee-gb

private void loadClock(long[] clockData, InputStream is) throws IOException {
  byte[] byteBuff = new byte[4 * clockData.length];
  IOUtils.read(is, byteBuff);
  ByteBuffer buff = ByteBuffer.wrap(byteBuff);
  buff.order(ByteOrder.LITTLE_ENDIAN);
  int i = 0;
  while (buff.hasRemaining()) {
    clockData[i++] = buff.getInt() & 0xffffffff;
  }
}

代码示例来源:origin: opensourceBIM/BIMserver

public static byte[] extractHead(BufferedInputStream bufferedInputStream, int maxSize) throws IOException {
    bufferedInputStream.mark(maxSize);
    byte[] initialBytes = new byte[maxSize];
    int read = IOUtils.read(bufferedInputStream, initialBytes);
    if (read != maxSize) {
      byte[] trimmed = new byte[read];
      System.arraycopy(initialBytes, 0, trimmed, 0, read);
      initialBytes = trimmed;
    }
    bufferedInputStream.reset();
    return initialBytes;
  }
}

代码示例来源:origin: org.nuxeo.ecm.core/nuxeo-core-api

IOUtils.read(in, magic);
if (!Arrays.equals(magic, FILE_MAGIC)) {
  throw new IOException("Invalid file (bad magic)");

代码示例来源:origin: org.apache.tika/tika-parsers

boolean read(InputStream is) throws IOException {
  bytesReadLast = IOUtils.read(is, bytes);
  if (DBFReader.STRICT && bytesReadLast != bytes.length) {
    throw new IOException("Truncated record, only read "+bytesReadLast+
        " bytes, but should have read: "+bytes.length);
  }
  return bytesReadLast > 0;
}

代码示例来源:origin: com.github.lafa.tikaNoExternal/tika-parsers

boolean read(InputStream is) throws IOException {
  bytesReadLast = IOUtils.read(is, bytes);
  if (DBFReader.STRICT && bytesReadLast != bytes.length) {
    throw new IOException("Truncated record, only read "+bytesReadLast+
        " bytes, but should have read: "+bytes.length);
  }
  return bytesReadLast > 0;
}

代码示例来源:origin: org.sonarsource.sonarqube/sonar-scanner-engine

private byte[] readBuffer() throws IOException {
 stream = new BufferedInputStream(Files.newInputStream(filePath), BYTES_TO_DECODE * 2);
 stream.mark(BYTES_TO_DECODE);
 byte[] buf = new byte[BYTES_TO_DECODE];
 int read = IOUtils.read(stream, buf, 0, BYTES_TO_DECODE);
 stream.reset();
 stream.mark(-1);
 return Arrays.copyOf(buf, read);
}

代码示例来源:origin: landy8530/DesignPatterns

public static String loadFileContentByClassPath(String fileName) throws IOException{
  Resource[] resources = getResources(fileName);
  if (resources.length == 0) {
    throw new FileNotFoundException(fileName);
  }
  InputStream io = resources[0].getInputStream();
  byte[] contents = new byte[102400];
  IOUtils.read(io, contents);
  IOUtils.closeQuietly(io);
  return new String(contents);
}

代码示例来源:origin: org.echocat.marquardt/common

private byte[] readSignableBytesAgainForLaterValidation(final CountingInputStream bufferedInputStream) throws IOException {
    final int position = (int)bufferedInputStream.getCount();
    bufferedInputStream.reset();
    final byte[] bytes = new byte[position];
    IOUtils.read(bufferedInputStream, bytes, 0, position);
    return bytes;
  }
}

代码示例来源:origin: org.mule.connectors/mule-ftp-connector

@Test
public void readBinary() throws Exception {
 testHarness.createBinaryFile();
 Message response = readPath(BINARY_FILE_NAME, false);
 assertThat(response.getPayload().getDataType().getMediaType().getPrimaryType(), is(MediaType.BINARY.getPrimaryType()));
 assertThat(response.getPayload().getDataType().getMediaType().getSubType(), is(MediaType.BINARY.getSubType()));
 AbstractFileInputStream payload = (AbstractFileInputStream) response.getPayload().getValue();
 assertThat(payload.isLocked(), is(false));
 byte[] readContent = new byte[new Long(HELLO_WORLD.length()).intValue()];
 org.apache.commons.io.IOUtils.read(payload, readContent);
 assertThat(new String(readContent), is(HELLO_WORLD));
}

相关文章