com.twelvemonkeys.io.FileUtil.read()方法的使用及代码示例

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

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

FileUtil.read介绍

[英]Gets the contents of the given file, as a byte array.
[中]以字节数组的形式获取给定文件的内容。

代码示例

代码示例来源:origin: haraldk/TwelveMonkeys

/**
 * Gets the contents of the given file, as a byte array.
 *
 * @param pFilename the name of the file to get content from
 * @return the content of the file as a byte array.
 * @throws IOException if the read operation fails
 */
public static byte[] read(String pFilename) throws IOException {
  return read(new File(pFilename));
}

代码示例来源:origin: haraldk/TwelveMonkeys

/**
 * Reads the response message from a file in the current web app.
 *
 * @param pFileName
 * @return the message
 */
private String readMessage(String pFileName) {
  try {
    // Read resource from web app
    InputStream is = getServletContext().getResourceAsStream(pFileName);
    if (is != null) {
      return new String(FileUtil.read(is));
    }
    else {
      log("File not found: " + pFileName);
    }
  }
  catch (IOException ioe) {
    log("Error reading file: " + pFileName + " (" + ioe.getMessage() + ")");
  }
  return null;
}

代码示例来源:origin: haraldk/TwelveMonkeys

if (content != null && content.exists()) {
  byte[] contents = FileUtil.read(content);

代码示例来源:origin: haraldk/TwelveMonkeys

protected static void markReset(SeekableInputStream pSeekable) throws IOException {
  for (int i = 0; i < 10; i++) {
    pSeekable.mark();
    System.out.println(new String(FileUtil.read(pSeekable)));
    pSeekable.reset();
  }
  System.out.println();
  System.out.println("mark/reset ok!");
}

代码示例来源:origin: haraldk/TwelveMonkeys

protected static void timeRead(SeekableInputStream pSeekable) throws IOException {
  for (int i = 0; i < 5000; i++) {
    pSeekable.mark();
    FileUtil.read(pSeekable);
    pSeekable.reset();
  }
  long start = System.currentTimeMillis();
  final int times = 200000;
  for (int i = 0; i < times; i++) {
    pSeekable.mark();
    FileUtil.read(pSeekable);
    pSeekable.reset();
  }
  long time = System.currentTimeMillis() - start;
  System.out.println("Time; " + time + "ms (" + (time / (float) times) + "ms/inv)");
}

代码示例来源:origin: haraldk/TwelveMonkeys

protected void testSeekSkip(SeekableInputStream pSeekable, String pStr) throws IOException {
  System.out.println();
  pSeekable.seek(pStr.length());
  FileUtil.read(pSeekable);
  for (int i = 0; i < 10; i++) {
    byte[] bytes = FileUtil.read(pSeekable);
    int len = bytes.length;
    if (len != 0) {
      System.err.println("Error in buffer length after full read...");
      System.err.println("len: " + len);
      System.err.println("bytes: \"" + new String(bytes) + "\"");
      break;
    }
  }
  System.out.println();
  for (int i = 0; i < 10; i++) {
    pSeekable.seek(0);
    int skip = i * 3;
    //noinspection ResultOfMethodCallIgnored
    pSeekable.skip(skip);
    String str = new String(FileUtil.read(pSeekable));
    System.out.println(str);
    if (str.length() != pStr.length() - skip) {
      throw new Error("Error in buffer length after skip");
    }
  }
  System.out.println();
  System.out.println("seek/skip ok!");
  System.out.println();
}

代码示例来源:origin: haraldk/TwelveMonkeys

private void runStreamTest(int pLength) throws Exception {
  byte[] data = createData(pLength);
  ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
  OutputStream out = new EncoderStream(outBytes, createCompatibleEncoder(), true);
  out.write(data);
  out.close();
  byte[] encoded = outBytes.toByteArray();
  byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()));
  assertArrayEquals(String.format("Data %d", pLength), data, decoded);
  InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder());
  outBytes = new ByteArrayOutputStream();
  FileUtil.copy(in, outBytes);
  outBytes.close();
  in.close();
  decoded = outBytes.toByteArray();
  assertArrayEquals(String.format("Data %d", pLength), data, decoded);
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testNoOpResponse() throws IOException {
  FastByteArrayOutputStream out = new FastByteArrayOutputStream(STREAM_DEFAULT_SIZE);
  
  HttpServletResponse response = mock(HttpServletResponse.class);
  when(response.getOutputStream()).thenReturn(new OutputStreamAdapter(out));
  ImageServletResponseImpl imageResponse = new ImageServletResponseImpl(request, response, context);
  fakeResponse(request, imageResponse);
  // TODO: Is there a way we can avoid calling flush?
  // Flush image to wrapped response
  imageResponse.flush();
  assertTrue("Content has no data", out.size() > 0);
  // Test that image data is untouched
  assertTrue("Data differs", Arrays.equals(FileUtil.read(getClass().getResourceAsStream(IMAGE_NAME_PNG)), out.toByteArray()));
  verify(response).setContentType(CONTENT_TYPE_PNG);
  verify(response).getOutputStream();
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
  public void testCreateInputStream() throws IOException {
    FastByteArrayOutputStream out = makeObject();

    String hello = "Hello World";
    out.write(hello.getBytes("UTF-8"));

    InputStream in = out.createInputStream();

    byte[] read = FileUtil.read(in);

    assertEquals(hello, new String(read, "UTF-8"));
  }
}

代码示例来源:origin: haraldk/TwelveMonkeys

data = FileUtil.read(stream1);
FileUtil.write(file1, data);
stream1.close();
data = FileUtil.read(stream2);
FileUtil.write(file2, data);
stream2.close();
data = FileUtil.read(stream3);
FileUtil.write(file3, data);
stream3.close();

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testSplit() throws IOException {
  InputStream inputStream = getClassLoaderResource("/contrib/tiff/multipage.tif").openStream();
  File inputFile = File.createTempFile("imageiotest", "tif");
  byte[] data = FileUtil.read(inputStream);
  FileUtil.write(inputFile, data);
  inputStream.close();
  File outputDirectory = Files.createTempDirectory("imageio").toFile();
  TIFFUtilities.split(inputFile, outputDirectory);
  ImageReader reader = ImageIO.getImageReadersByFormatName("TIF").next();
  File[] outputFiles = outputDirectory.listFiles();
  Assert.assertEquals(3, outputFiles.length);
  for (File outputFile : outputFiles) {
    ImageInputStream iis = ImageIO.createImageInputStream(outputFile);
    reader.setInput(iis);
    Assert.assertEquals(1, reader.getNumImages(true));
    iis.close();
    outputFile.delete();
  }
  outputDirectory.delete();
  inputFile.delete();
}

代码示例来源:origin: haraldk/TwelveMonkeys

byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder()));
assertTrue(Arrays.equals(data, decoded));

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testApplyOrientation() throws IOException {
  InputStream inputStream = getClassLoaderResource("/contrib/tiff/multipage.tif").openStream();
  File inputFile = File.createTempFile("imageiotest", "tif");
  byte[] data = FileUtil.read(inputStream);
  FileUtil.write(inputFile, data);
  inputStream.close();
  BufferedImage image = ImageIO.read(inputFile);
  // rotate by 90�
  BufferedImage image90 = TIFFUtilities.applyOrientation(image, TIFFExtension.ORIENTATION_RIGHTTOP);
  // rotate by 270�
  BufferedImage image360 = TIFFUtilities.applyOrientation(image90, TIFFExtension.ORIENTATION_LEFTBOT);
  byte[] original = ((DataBufferByte) image.getData().getDataBuffer()).getData();
  byte[] rotated = ((DataBufferByte) image360.getData().getDataBuffer()).getData();
  Assert.assertArrayEquals(original, rotated);
}

代码示例来源:origin: haraldk/TwelveMonkeys

byte[] data = FileUtil.read(inputStream);
FileUtil.write(inputFile, data);
inputStream.close();

代码示例来源:origin: com.github.lafa.twelvemonkeyspurejava.common/common-io

/**
 * Gets the contents of the given file, as a byte array.
 *
 * @param pFilename the name of the file to get content from
 * @return the content of the file as a byte array.
 * @throws IOException if the read operation fails
 */
public static byte[] read(String pFilename) throws IOException {
  return read(new File(pFilename));
}

代码示例来源:origin: com.twelvemonkeys/twelvemonkeys-core

/**
 * Gets the contents of the given file, as a byte array.
 *
 * @param pFilename the name of the file to get content from
 * @return the content of the file as a byte array.
 * @throws IOException if the read operation fails
 */
public static byte[] read(String pFilename) throws IOException {
  return read(new File(pFilename));
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testSeekSkipRead() throws Exception {
  SeekableInputStream seekable = makeInputStream(133);
  int pos = 45;
  for (int i = 0; i < 10; i++) {
    seekable.seek(pos);
    //noinspection ResultOfMethodCallIgnored
    seekable.skip(i);
    byte[] bytes = FileUtil.read(seekable);
    assertEquals(133, seekable.getStreamPosition());
    assertEquals(133 - 45- i, bytes.length);
  }
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testCloseUnderlyingStream() throws IOException {
  final boolean[] closed = new boolean[1];
  ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) {
    @Override
    public void close() throws IOException {
      closed[0] = true;
      super.close();
    }
  };
  SeekableInputStream stream = makeInputStream(input);
  try {
    FileUtil.read(stream); // Read until EOF
    assertEquals("EOF not reached (test case broken)", -1, stream.read());
    assertFalse("Underlying stream closed before close", closed[0]);
  }
  finally {
    stream.close();
  }
  assertTrue("Underlying stream not closed", closed[0]);
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
  public void testCloseUnderlyingFile() throws IOException {
    final boolean[] closed = new boolean[1];

    File file = createFileWithContent(new ByteArrayInputStream(makeRandomArray(256)));

    RandomAccessFile raf = new RandomAccessFile(file, "r") {
      @Override
      public void close() throws IOException {
        closed[0] = true;
        super.close();
      }
    };

    FileSeekableStream stream = new FileSeekableStream(raf);

    try {
      FileUtil.read(stream); // Read until EOF

      assertEquals("EOF not reached (test case broken)", -1, stream.read());
      assertFalse("Underlying stream closed before close", closed[0]);
    }
    finally {
      stream.close();
    }

    assertTrue("Underlying stream not closed", closed[0]);
  }
}

代码示例来源:origin: com.twelvemonkeys/twelvemonkeys-core

public void testSeekSkipRead() throws Exception {
  SeekableInputStream seekable = makeInputStream(133);
  int pos = 45;
  for (int i = 0; i < 10; i++) {
    seekable.seek(pos);
    //noinspection ResultOfMethodCallIgnored
    seekable.skip(i);
    byte[] bytes = FileUtil.read(seekable);
    assertEquals(133, seekable.getStreamPosition());
    assertEquals(133 - 45- i, bytes.length);
  }
}

相关文章