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

x33g5p2x  于2022-01-18 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(1466)

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

FileUtils.readFileToByteArray介绍

[英]Reads the contents of a file into a byte array. The file is always closed.
[中]将文件内容读入字节数组。文件始终处于关闭状态。

代码示例

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

private static InputStream toInputStream(File storeFile)
   throws IOException {
  byte[] data = FileUtils.readFileToByteArray(storeFile);
  return new ByteArrayInputStream(data);
 }
}

代码示例来源:origin: ming1016/study

@Nullable
public static byte[] getBytesFromFile(@NotNull String filename) {
  try {
    return FileUtils.readFileToByteArray(new File(filename));
  } catch (Exception e) {
    return null;
  }
}

代码示例来源:origin: ming1016/study

@Nullable
public static byte[] getBytesFromFile(@NotNull String filename) {
  try {
    return FileUtils.readFileToByteArray(new File(filename));
  } catch (Exception e) {
    return null;
  }
}

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

public FileResultModel(File file, int fileType) {
 if (fileType != FILE_TYPE_BINARY && fileType != FILE_TYPE_TEXT) {
  throw new IllegalArgumentException("Unsupported file type is specified.");
 }
 this.filename = file.getName();
 try {
  this.data = FileUtils.readFileToByteArray(file);
 } catch (IOException e) {
  throw new RuntimeException("Unable to read file: " + file.getAbsolutePath(), e);
 }
 this.length = data.length;
 this.type = fileType;
}

代码示例来源:origin: alibaba/jstorm

@SuppressWarnings("unchecked")
public static Object readLocalObject(String topologyId, String readFile) throws IOException {
  String errMsg = "Failed to get topology configuration of " + topologyId + " file:" + readFile;
  byte[] bconf = FileUtils.readFileToByteArray(new File(readFile));
  if (bconf == null) {
    errMsg += ", failed to read";
    LOG.error(errMsg);
    throw new IOException(errMsg);
  }
  Object ret;
  try {
    ret = Utils.javaDeserialize(bconf);
  } catch (Exception e) {
    errMsg += ", failed to serialize the data";
    LOG.error(errMsg);
    throw new IOException(errMsg);
  }
  return ret;
}

代码示例来源:origin: mpetazzoni/ttorrent

public TorrentMetadata parseFromFile(File torrentFile) throws IOException {
 byte[] fileContent = FileUtils.readFileToByteArray(torrentFile);
 return parse(fileContent);
}

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

private static void addCompiledClasses(List<CompiledSourceCode> ret, String pkgName, File dir)
  throws IOException {
 for (File file : dir.listFiles()) {
  String filename = file.getName();
  if (file.isDirectory()) {
   String qname = pkgName + filename + ".";
   addCompiledClasses(ret, qname, file);
  } else if (filename.endsWith(".class")) {
   String qname = pkgName + filename.substring(0, filename.length() - 6);
   ret.add(new CompiledSourceCode(qname, FileUtils.readFileToByteArray(file)));
  } else {
   System.err.println("Unexpected file : " + file.getAbsolutePath());
  }
 }
}

代码示例来源:origin: alibaba/mdrill

public synchronized Map<Object, Object> snapshot() throws IOException {
  String latestPath = _vs.mostRecentVersionPath();
  if(latestPath==null) return new HashMap<Object, Object>();
  return (Map<Object, Object>) Utils.deserialize(FileUtils.readFileToByteArray(new File(latestPath)));
}

代码示例来源:origin: alibaba/mdrill

public static Map read_supervisor_storm_conf(Map conf, String storm_id)
    throws IOException {
  String stormroot = StormConfig
      .supervisor_stormdist_root(conf, storm_id);
  String conf_path = StormConfig.supervisor_sotrmconf_path(stormroot);
  // String topology_path =
  // StormConfig.supervisor_stormcode_path(stormroot);
  Map rtn = new HashMap();
  rtn.putAll(conf);
  rtn.putAll((Map) Utils.deserialize(FileUtils
      .readFileToByteArray(new File(conf_path))));
  return rtn;
}

代码示例来源:origin: alibaba/jstorm

public static long read_supervisor_topology_timestamp(Map conf, String topologyId) throws IOException {
  String stormRoot = supervisor_stormdist_root(conf, topologyId);
  String timeStampPath = stormts_path(stormRoot);
  byte[] data = FileUtils.readFileToByteArray(new File(timeStampPath));
  return JStormUtils.bytesToLong(data);
}

代码示例来源:origin: alibaba/jstorm

public static long read_nimbus_topology_timestamp(Map conf, String topologyId) throws IOException {
  String stormRoot = masterStormdistRoot(conf, topologyId);
  String timeStampPath = stormts_path(stormRoot);
  byte[] data = FileUtils.readFileToByteArray(new File(timeStampPath));
  return JStormUtils.bytesToLong(data);
}

代码示例来源:origin: alibaba/mdrill

public static StormTopology read_supervisor_topology(Map conf,
    String topologyid) throws IOException {
  String topologyroot = StormConfig.supervisor_stormdist_root(conf,
      topologyid);
  String topology_path = StormConfig
      .supervisor_stormcode_path(topologyroot);
  return (StormTopology) Utils.deserialize(FileUtils
      .readFileToByteArray(new File(topology_path)));
}

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

@Test
public void testCopyFileToOutputStream() throws Exception {
  final ByteArrayOutputStream destination = new ByteArrayOutputStream();
  FileUtils.copyFile(testFile1, destination);
  assertEquals("Check Full copy size", testFile1Size, destination.size());
  final byte[] expected = FileUtils.readFileToByteArray(testFile1);
  Assert.assertArrayEquals("Check Full copy", expected, destination.toByteArray());
}

代码示例来源:origin: alibaba/jstorm

private Map<Object, Object> deserializeLatestVersion() throws IOException {
  String latestPath = _vs.mostRecentVersionPath();
  Long latestVersion = _vs.mostRecentVersion();
  Map<Object, Object> result = new HashMap<>();
  while (latestPath != null) {
    byte[] serialized = FileUtils.readFileToByteArray(new File(latestPath));
    if (serialized.length == 0) {
      LOG.warn("LocalState file '{}' contained no data, skip this state", latestPath);
      latestPath = _vs.mostRecentVersionPath(latestVersion - 1);
      latestVersion = _vs.mostRecentVersion(latestVersion - 1);
    } else {
      result = (Map<Object, Object>) Utils.javaDeserialize(serialized);
      break;
    }
  }
  return result;
}

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

@Override
public void run(String[] args, Map<String, Object> conf, String command) throws Exception {
  for (String arg: args) {
    System.out.println(arg + ":");
    StormTopology topo;
    File f = new File(arg);
    if (f.exists()) {
      topo = Utils.deserialize(FileUtils.readFileToByteArray(f), StormTopology.class);
    } else { //assume it is a topology id
      final String key = ConfigUtils.masterStormCodeKey(arg);
      try (BlobStore store = ServerUtils.getNimbusBlobStore(conf, NimbusInfo.fromConf(conf), null)) {
        topo = Utils.deserialize(store.readBlob(key, Nimbus.NIMBUS_SUBJECT), StormTopology.class);
      }
    }
    System.out.println(prettyPrint(topo));
  }
}

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

private static void verifySameContent(TestResponse response, File file) throws IOException {
  assertThat(IOUtils.toByteArray(response.getInputStream())).isEqualTo(FileUtils.readFileToByteArray(file));
 }
}

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

@Test
public void testReadFileToByteArray() throws Exception {
  final File file = new File(getTestDirectory(), "read.txt");
  final FileOutputStream out = new FileOutputStream(file);
  out.write(11);
  out.write(21);
  out.write(31);
  out.close();
  final byte[] data = FileUtils.readFileToByteArray(file);
  assertEquals(3, data.length);
  assertEquals(11, data[0]);
  assertEquals(21, data[1]);
  assertEquals(31, data[2]);
}

代码示例来源:origin: mpetazzoni/ttorrent

public static SharedTorrent fromFile(File source, PieceStorage pieceStorage, TorrentStatistic torrentStatistic)
    throws IOException {
 byte[] data = FileUtils.readFileToByteArray(source);
 TorrentMetadata torrentMetadata = new TorrentParser().parse(data);
 return new SharedTorrent(torrentMetadata, pieceStorage, DEFAULT_REQUEST_STRATEGY, torrentStatistic, new EventDispatcher());
}

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

trustStorePwdBytes = FileUtils.readFileToByteArray(serverSslConfig.getClientAuthTrustStorePasswordFile());

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

/**
 * Enqueue download of file with a MD5 that may not be returned (null) or not valid
 */
private void enqueueDownload(File file, @Nullable String md5) throws IOException {
 Buffer body = new Buffer();
 body.write(FileUtils.readFileToByteArray(file));
 MockResponse response = new MockResponse().setBody(body);
 if (md5 != null) {
  response.setHeader("Sonar-MD5", md5);
 }
 server.enqueue(response);
}

相关文章

FileUtils类方法