org.apache.poi.util.IOUtils.toByteArray()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(1958)

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

IOUtils.toByteArray介绍

[英]Reads all the data from the input stream, and returns the bytes read.
[中]从输入流读取所有数据,并返回读取的字节。

代码示例

代码示例来源:origin: org.apache.poi/poi

/**
 * Reads up to {@code length} bytes from the input stream, and returns the bytes read.
 *
 * @param stream The byte stream of data to read.
 * @param length The maximum length to read, use Integer.MAX_VALUE to read the stream
 *               until EOF.
 * @return A byte array with the read bytes.
 * @throws IOException If reading data fails or EOF is encountered too early for the given length.
 */
public static byte[] toByteArray(InputStream stream, final int length) throws IOException {
  return toByteArray(stream, length, Integer.MAX_VALUE);
}

代码示例来源:origin: org.apache.poi/poi

/**
 * Reads all the data from the input stream, and returns the bytes read.
 *
 * @param stream The byte stream of data to read.
 * @return A byte array with the read bytes.
 * @throws IOException If reading data fails or EOF is encountered too early for the given length.
 */
public static byte[] toByteArray(InputStream stream) throws IOException {
  return toByteArray(stream, Integer.MAX_VALUE);
}

代码示例来源:origin: org.apache.poi/poi

public HeaderBlock(ByteBuffer buffer) throws IOException {
  this(IOUtils.toByteArray(buffer, POIFSConstants.SMALLER_BIG_BLOCK_SIZE));
}

代码示例来源:origin: org.apache.poi/poi-ooxml

ZipArchiveFakeEntry(ZipArchiveEntry entry, InputStream inp) throws IOException {
  super(entry.getName());
  final long entrySize = entry.getSize();
  if (entrySize < -1 || entrySize>=Integer.MAX_VALUE) {
    throw new IOException("ZIP entry size is too large or invalid");
  }
  // Grab the de-compressed contents for later
  data = (entrySize == -1) ? IOUtils.toByteArray(inp) : IOUtils.toByteArray(inp, (int)entrySize);
}

代码示例来源:origin: org.apache.poi/poi

/**
 * Convenience method to get the embedded data as byte array.
 *
 * @return the embedded data.
 */
default byte[] getBytes() throws IOException {
  try (InputStream is = getInputStream()) {
    return IOUtils.toByteArray(is);
  }
}

代码示例来源:origin: org.apache.poi/poi

public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(args[0]);
    byte[] b = IOUtils.toByteArray(in);
    in.close();
    System.out.println(HexDump.dump(b, 0, 0));
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Adds a picture to the slideshow.
 *
 * @param is     The stream to read image from
 * @param format The format of the picture
 * @return the picture data
 * @since 3.15 beta 2
 */
@Override
public XSLFPictureData addPicture(InputStream is, PictureType format) throws IOException {
  return addPicture(IOUtils.toByteArray(is), format);
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Adds a picture to the document.
 *
 * @param is     The stream to read image from
 * @param format The format of the picture.
 * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
 * @throws InvalidFormatException If the format of the picture is not known.
 * @throws IOException If reading the picture-data from the stream fails.
 */
public String addPictureData(InputStream is, int format) throws InvalidFormatException, IOException {
  byte[] data = IOUtils.toByteArray(is);
  return addPictureData(data, format);
}

代码示例来源:origin: org.apache.poi/poi

/**
 * Get the file magic of the supplied {@link File}<p>
 *
 * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean,
 *  that the ZIP stream has leading junk bytes
 *
 * @param inp a file to be identified
 */
public static FileMagic valueOf(final File inp) throws IOException {
  try (FileInputStream fis = new FileInputStream(inp)) {
    final byte[] data = IOUtils.toByteArray(fis, 8);
    return FileMagic.valueOf(data);
  }
}

代码示例来源:origin: org.apache.poi/poi

private static void readEntry(POIFSReaderEvent event) {
  POIFSDocumentPath path = event.getPath();
  StringBuilder sb = new StringBuilder();
  try (DocumentInputStream istream = event.getStream()) {
    sb.setLength(0);
    int pathLength = path.length();
    for (int k = 0; k < pathLength; k++) {
      sb.append("/").append(path.getComponent(k));
    }
    byte[] data = IOUtils.toByteArray(istream);
    sb.append("/").append(event.getName()).append(": ").append(data.length).append(" bytes read");
    System.out.println(sb);
  } catch (IOException ignored) {
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

public String addPictureData(InputStream is, int format) throws InvalidFormatException {
  try {
    byte[] data = IOUtils.toByteArray(is);
    return addPictureData(data, format);
  } catch (IOException e) {
    throw new POIXMLException(e);
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Gets the picture data as a byte array.
 *
 * You can grab the picture data directly from the underlying package part with the {@link #getInputStream()} method
 *
 * @return the Picture data.
 */
public byte[] getData() {
  try {
    return IOUtils.toByteArray(getInputStream());
  } catch (IOException e) {
    throw new POIXMLException(e);
  }
}

代码示例来源:origin: org.apache.poi/poi

public static void dump(DirectoryEntry root, File parent) throws IOException {
  for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
    Entry entry = it.next();
    if(entry instanceof DocumentNode){
      DocumentNode node = (DocumentNode)entry;
      DocumentInputStream is = new DocumentInputStream(node);
      byte[] bytes = IOUtils.toByteArray(is);
      is.close();
      try (OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()))) {
        out.write(bytes);
      }
    } else if (entry instanceof DirectoryEntry){
      DirectoryEntry dir = (DirectoryEntry)entry;
      File file = new File(parent, entry.getName());
      if(!file.exists() && !file.mkdirs()) {
        throw new IOException("Could not create directory " + file);
      }
      dump(dir, file);
    } else {
      System.err.println("Skipping unsupported POIFS entry: " + entry);
    }
  }
}
public static void dump(POIFSFileSystem fs, int startBlock, String name, File parent) throws IOException {

代码示例来源:origin: org.apache.poi/poi-ooxml

public Long getChecksum() {
  if (this.checksum == null) {
    byte[] data;
    try (InputStream is = getPackagePart().getInputStream()) {
      data = IOUtils.toByteArray(is);
    } catch (IOException e) {
      throw new POIXMLException(e);
    }
    this.checksum = IOUtils.calculateChecksum(data);
  }
  return this.checksum;
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Gets the picture data as a byte array.
 * <p>
 * Note, that this call might be expensive since all the picture data is copied into a temporary byte array.
 * You can grab the picture data directly from the underlying package part as follows:
 * <br>
 * <code>
 * InputStream is = getPackagePart().getInputStream();
 * </code>
 * </p>
 *
 * @return the Picture data.
 */
public byte[] getData() {
  try {
    return IOUtils.toByteArray(getPackagePart().getInputStream());
  } catch (IOException e) {
    throw new POIXMLException(e);
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Gets the picture data as a byte array.
 * <p>
 * Note, that this call might be expensive since all the picture data is copied into a temporary byte array.
 * You can grab the picture data directly from the underlying package part as follows:
 * <br>
 * <code>
 * InputStream is = getPackagePart().getInputStream();
 * </code>
 * </p>
 *
 * @return the picture data.
 */
public byte[] getData() {
  try {
    return IOUtils.toByteArray(getPackagePart().getInputStream());
  } catch(IOException e) {
    throw new POIXMLException(e);
  }
}

代码示例来源:origin: org.apache.poi/poi

/**
 * Creates a {@link PropertySet} instance from an {@link
 * InputStream} in the Horrible Property Set Format.<p>
 *
 * The constructor reads the first few bytes from the stream
 * and determines whether it is really a property set stream. If
 * it is, it parses the rest of the stream. If it is not, it
 * resets the stream to its beginning in order to let other
 * components mess around with the data and throws an
 * exception.
 *
 * @param stream Holds the data making out the property set
 * stream.
 * @throws IOException
 *    if the {@link InputStream} cannot be accessed as needed.
 * @exception NoPropertySetStreamException
 *    if the input stream does not contain a property set.
 * @exception UnsupportedEncodingException
 *    if a character encoding is not supported.
 */
public PropertySet(final InputStream stream)
throws NoPropertySetStreamException, IOException {
  if (!isPropertySetStream(stream)) {
    throw new NoPropertySetStreamException();
  }
  
  final byte[] buffer = IOUtils.toByteArray(stream);
  init(buffer, 0, buffer.length);
}

代码示例来源:origin: org.apache.poi/poi-ooxml

public Long getChecksum() {
  if (this.checksum == null) {
    InputStream is = null;
    byte[] data;
    try {
      is = getPackagePart().getInputStream();
      data = IOUtils.toByteArray(is);
    } catch (IOException e) {
      throw new POIXMLException(e);
    } finally {
      IOUtils.closeQuietly(is);
    }
    this.checksum = IOUtils.calculateChecksum(data);
  }
  return this.checksum;
}

代码示例来源:origin: org.apache.poi/poi

byte[] data = IOUtils.toByteArray(is);
  HexDump.dump(data, 0, System.out, 0);
} else {

代码示例来源:origin: org.apache.poi/poi

@Override
  public EmbeddedData extract(DirectoryNode dn) throws IOException {
    ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid());
    String contentType = null;
    String ext = null;
    
    if (clsId != null) {
      contentType = clsId.getContentType();
      ext = clsId.getFileExtension();
    }
    
    if (contentType == null || ext == null) {
      contentType = "application/zip";
      ext = ".zip";
    }
    DocumentInputStream dis = dn.createDocumentInputStream("package");
    byte data[] = IOUtils.toByteArray(dis);
    dis.close();
    
    return new EmbeddedData(dn.getName()+ext, data, contentType);
  }
}

相关文章