Java ByteArrayOutputStream Write loadIfileAsBytes(final IFile ifile)

Here you can find the source of loadIfileAsBytes(final IFile ifile)

Description

Helper to read a ifile from the workspace into a byte array.

License

Open Source License

Parameter

Parameter Description
ifile The ifile object

Exception

Parameter Description
CoreException A
IOException B

Return

The file's contents

Declaration

public static byte[] loadIfileAsBytes(final IFile ifile) throws CoreException, IOException 

Method Source Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

public class Main {
    /**/* ww w  .  j av  a  2  s . c om*/
     * The buffer size.
     */
    private static final int BUF_SIZE = 1024 * 8;

    /**
     * Helper to read a ifile from the workspace into a byte array.
     * 
     * @param ifile The ifile object
     * @return The file's contents
     * @throws CoreException A
     * @throws IOException B
     */
    public static byte[] loadIfileAsBytes(final IFile ifile) throws CoreException, IOException {
        final InputStream is = ifile.getContents();
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final byte[] buffer = new byte[BUF_SIZE];
        int num;
        while ((num = is.read(buffer)) > 0) {
            baos.write(buffer, 0, num);
        }
        is.close();
        baos.close(); // effect-less
        return baos.toByteArray();
    }
}

Related

  1. loadAFileToString(File f)
  2. loadAsciiTxt()
  3. loadAsText(Class cls, String name)
  4. loadBinFile(java.io.File f)
  5. loadFully(final InputStream aStream)
  6. loadIntoMemory(InputStream is)
  7. loadProbe(InputStream in, int buffSize)
  8. loadStream(InputStream in, String encoding)
  9. readAll(File file)