Example usage for java.io BufferedInputStream available

List of usage examples for java.io BufferedInputStream available

Introduction

In this page you can find the example usage for java.io BufferedInputStream available.

Prototype

public synchronized int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

Usage

From source file:org.openflamingo.uploader.util.ResourceUtils.java

/**
 *  ?  {@link org.springframework.core.io.Resource}? ? ? .
 *
 * @param inputStream   /*from   w  w w .j  a va 2s  .  c o  m*/
 * @return {@link org.springframework.core.io.Resource}? ? 
 * @throws java.io.IOException  ?    
 */
public static String getResourceTextContents(InputStream inputStream) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    int size = bis.available();
    byte[] bytes = new byte[size];
    bis.read(bytes);
    return new String(bytes, "UTF-8");
}

From source file:com.github.pitzcarraldo.openjpeg.OpenJPEGLoader.java

private static void copyStreamToFile(InputStream pInputStream, File pFile) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(pInputStream);
    FileOutputStream fos = new FileOutputStream(pFile.getAbsolutePath());
    byte[] buffer = new byte[32768];
    int bytesRead = 0;
    while (bis.available() > 0) {
        bytesRead = bis.read(buffer);//ww  w .  jav  a 2s  . c om
        fos.write(buffer, 0, bytesRead);
    }
    bis.close();
    fos.close();
    pFile.deleteOnExit();
}

From source file:com.sangupta.clitools.file.HexDump.java

public static void hexDump(PrintStream outStream, BufferedInputStream bis, int currentRow, int maxRows)
        throws IOException {
    int row = currentRow + 1;
    if (maxRows == 0) {
        maxRows = Integer.MAX_VALUE;
    } else {/*from w w w .  j a  va 2s  . com*/
        maxRows += currentRow;
    }

    StringBuilder builder1 = new StringBuilder(100);
    StringBuilder builder2 = new StringBuilder(100);

    while (bis.available() > 0) {
        outStream.printf("%04X  ", row * 16);
        for (int j = 0; j < 16; j++) {
            if (bis.available() > 0) {
                int value = (int) bis.read();
                builder1.append(String.format("%02X ", value));

                if (!Character.isISOControl(value)) {
                    builder2.append((char) value);
                } else {
                    builder2.append(".");
                }
            } else {
                for (; j < 16; j++) {
                    builder1.append("   ");
                }
            }
        }
        outStream.print(builder1);
        outStream.println(builder2);
        row++;

        if (row > maxRows) {
            break;
        }

        builder1.setLength(0);
        builder2.setLength(0);
    }
}

From source file:pipeserver.Data.java

private static byte[] readInputStream(BufferedInputStream bin) throws IOException {
    if (bin == null) {
        logger.warning("BufferedInputStream was null ! ");
        return null;
    }/*from  w w w.  ja  va 2s  . c  o  m*/
    byte data[] = null;
    int s = bin.read();
    if (s == -1)
        return null; //Connection lost
    int alength = bin.available();
    if (alength > 0) {
        data = new byte[alength + 1];
        data[0] = (byte) s;
        bin.read(data, 1, alength);
    } else {
        data = new byte[1];
        data[0] = (byte) s;
    }
    return data;
}

From source file:com.avapira.bobroreader.Bober.java

public static String rawJsonToString(Resources res, @RawRes int resId) {
    String name = res.getResourceName(resId);
    BufferedInputStream bis = new BufferedInputStream(res.openRawResource(resId));
    try {//from   w  w  w .j a  va2s . co  m
        byte[] bytes = new byte[bis.available()];
        int bytesRead = bis.read(bytes);
        Log.i("Bober#rawJsonToString", String.format("Streaming raw file %s: %s bytes read", name, bytesRead));
        return new String(bytes);
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:org.wso2.msf4j.ballerina.Application.java

private static void unzip(String zipFilePath, String destDir) throws IOException {
    BufferedInputStream bis = null;
    FileOutputStream fileOutput = null;
    try (ZipFile file = new ZipFile(zipFilePath)) {
        FileSystem fileSystem = FileSystems.getDefault();
        Enumeration<? extends ZipEntry> entries = file.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                Files.createDirectories(fileSystem.getPath(destDir, entry.getName()));
            } else {
                InputStream is = file.getInputStream(entry);
                bis = new BufferedInputStream(is);
                Path uncompressedFilePath = fileSystem.getPath(destDir, entry.getName());
                Files.createFile(uncompressedFilePath);
                fileOutput = new FileOutputStream(uncompressedFilePath.toString());
                while (bis.available() > 0) {
                    fileOutput.write(bis.read());
                }//ww  w  . j a v  a  2 s  . c o m
                fileOutput.close();
            }
        }
    } finally {
        if (bis != null) {
            IOUtils.closeQuietly(bis);
        }
        if (fileOutput != null) {
            IOUtils.closeQuietly(fileOutput);
        }
    }
}

From source file:com.sangupta.jerry.util.FileUtils.java

/**
 * Dump a given file into HEX starting at given offset and reading given number of rows where
 * a row consists of 16-bytes.//from ww  w .ja v  a 2  s .co  m
 * 
 * @param out
 * @param file
 * @param offset
 * @param maxRows
 * @throws IOException
 */
public static void hexDump(PrintStream out, File file, long offset, int maxRows) throws IOException {
    InputStream is = null;
    BufferedInputStream bis = null;

    try {
        is = new FileInputStream(file);
        bis = new BufferedInputStream(is);
        bis.skip(offset);

        int row = 0;
        if (maxRows == 0) {
            maxRows = Integer.MAX_VALUE;
        }

        StringBuilder builder1 = new StringBuilder(100);
        StringBuilder builder2 = new StringBuilder(100);

        while (bis.available() > 0) {
            out.printf("%04X  ", row * 16);
            for (int j = 0; j < 16; j++) {
                if (bis.available() > 0) {
                    int value = (int) bis.read();
                    builder1.append(String.format("%02X ", value));

                    if (!Character.isISOControl(value)) {
                        builder2.append((char) value);
                    } else {
                        builder2.append(".");
                    }
                } else {
                    for (; j < 16; j++) {
                        builder1.append("   ");
                    }
                }
            }
            out.print(builder1);
            out.println(builder2);
            row++;

            if (row > maxRows) {
                break;
            }

            builder1.setLength(0);
            builder2.setLength(0);
        }
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(is);
    }
}

From source file:org.frc836.database.DB.java

public static void exportToCSV(Context context) {
    try {/* w w w.  j  a  v  a 2 s. c o m*/
        cb = new ExportCallback();
        String filename;
        try {
            BufferedInputStream bis = new BufferedInputStream(context.openFileInput(FILENAME));
            byte[] buffer = new byte[bis.available()];
            bis.read(buffer, 0, buffer.length);
            filename = new String(buffer);
        } catch (Exception e) {
            filename = null;
        }

        cb.context = context;

        new FileSelector(context, FileOperation.SELECTDIR, mDirSelectListener, null, filename).show();

    } catch (Exception e) {
        Toast.makeText(context, "Error exporting Database", Toast.LENGTH_LONG).show();
    }
}

From source file:net.dfs.remote.fileretrieve.impl.RetrievalManagerImpl.java

/**
 * Read the File from the local disk and send it to the Space.
 * Connect to the remote Space via {@link FileSpaceCreator}. Read the bites in 
 * the File into a InputStream and set the properties of the created File model. 
 * <p>//  w w w.  j  ava2s. c  om
 * File model properties are FileName, the size of the file in bites and the
 * actual bites read in the File. Finally send the wrapped File to the Space
 * via {@link FileSenderSupport}
 * <p>
 * IOException will be thrown on a failure.
 * {@inheritDoc}
 * @throws IOException 
 */
public FileRetrievalModel retrieveFile(String fileName) throws IOException {

    BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(new File(path + fileName)));

    byte[] buffer = new byte[inputStream.available()];
    Integer bytesRead = 0;

    FileRetrievalModel fileModel = new FileRetrievalModel();

    while ((bytesRead = inputStream.read(buffer)) != -1) {
        fileModel.fileName = fileName;
        fileModel.bytes = buffer;
        fileModel.bytesRead = bytesRead;
    }

    log.info("The File " + fileModel.fileName + " with bytes " + fileModel.bytesRead
            + " Sending back to the Server");
    return fileModel;
}

From source file:de.betterform.agent.web.servlet.XSLTServlet.java

private StringBuffer generateError(String error) throws IOException, XFormsConfigException {

    String path = WebFactory.getRealPath("forms/incubator/editor", getServletContext());
    File f = new File(path, "callerror.html");
    FileInputStream fs = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fs);
    int numBytes = bis.available();
    byte b[] = new byte[numBytes];
    // read the template into a StringBuffer (via a byte array)
    bis.read(b);//from ww  w . j  a v  a2 s .  c om
    StringBuffer buf = new StringBuffer();
    buf.append(b);
    int start = buf.indexOf(errorTemplate);
    int end = start + errorTemplate.length();
    // replace placeholder with errormessage
    // (will automatically adjust to take longer or shorter data into account)
    buf.replace(start, end, error);
    return buf;
}