Example usage for java.io DataInputStream readFully

List of usage examples for java.io DataInputStream readFully

Introduction

In this page you can find the example usage for java.io DataInputStream readFully.

Prototype

public final void readFully(byte b[], int off, int len) throws IOException 

Source Link

Document

See the general contract of the readFully method of DataInput .

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    InputStream is = new FileInputStream("c:\\test.txt");
    DataInputStream dis = new DataInputStream(is);

    int length = dis.available();

    byte[] buf = new byte[length];

    dis.readFully(buf, 4, 5);
    for (byte b : buf) {
        char c = '0';
        if (b != 0) {
            c = (char) b;
        }/*from w  w  w  .  ja v a2  s . com*/
        System.out.print(c);
    }

}

From source file:org.openspaces.core.util.FileUtils.java

public static byte[] unzipFileToMemory(String applicationFile, File directoryOrZip, long maxFileSizeInBytes) {
    ZipFile zipFile = null;/*w  ww. j a  v a 2s  . c  o  m*/
    try {
        zipFile = new ZipFile(directoryOrZip);
        final ZipEntry zipEntry = zipFile.getEntry(applicationFile);
        if (zipEntry == null) {
            throw new RuntimeException("Failed to load " + applicationFile + " from " + directoryOrZip);
        }
        final int length = (int) zipEntry.getSize();
        if (length > maxFileSizeInBytes) {
            throw new RuntimeException(
                    applicationFile + " file size cannot be bigger than " + maxFileSizeInBytes + " bytes");
        }
        byte[] buffer = new byte[length];
        final InputStream in = zipFile.getInputStream(zipEntry);
        final DataInputStream din = new DataInputStream(in);
        try {
            din.readFully(buffer, 0, length);
            return buffer;
        } catch (final IOException e) {
            throw new RuntimeException("Failed to read application file input stream", e);
        }

    } catch (final IOException e) {
        throw new RuntimeException("Failed to read zip file " + directoryOrZip, e);
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (final IOException e) {
                logger.debug("Failed to close zip file " + directoryOrZip, e);
            }
        }
    }
}

From source file:com.googlecode.jsendnsca.NagiosPassiveCheckSender.java

private static byte[] readFrom(DataInputStream inputStream) throws NagiosException, SocketTimeoutException {
    try {//  w  w w.j a v a  2 s  .c  om
        final byte[] initVector = new byte[INITIALISATION_VECTOR_SIZE];
        inputStream.readFully(initVector, 0, INITIALISATION_VECTOR_SIZE);
        return initVector;
    } catch (SocketTimeoutException ste) {
        throw ste;
    } catch (IOException e) {
        throw new NagiosException("Can't read initialisation vector", e);
    }
}

From source file:org.sakaiproject.content.impl.db.test.CheckBlobSafety.java

@Test
public void testBlob() {
    try {//from  ww  w  .ja  v a2s  . c  o  m
        Random r = new Random();
        int blockSize = 4095; // use an odd size to get byte boundaries
        int nblocks = 512;
        int maxSize = blockSize * nblocks;
        byte[] b = new byte[maxSize];
        byte[] bin = new byte[maxSize];
        log.info("Loading Random Data " + maxSize);
        r.nextBytes(b);
        log.info("Loaded Random Data");

        log.info("Got Connection");
        PreparedStatement pstout = null;
        PreparedStatement pstin = null;
        InputStream instream = null;
        ResultSet rs = null;
        try {
            pstout = con.prepareStatement(p.getProperty("insert.statement"));
            pstin = con.prepareStatement(p.getProperty("select.statement"));
            for (int i = 1; i < nblocks; i += 5) {
                int size = blockSize * i;
                pstout.clearParameters();

                pstout.setBinaryStream(1, new ByteArrayInputStream(b), size);
                pstout.setInt(2, i);
                pstout.executeUpdate();
                log.info("Loaded record  " + i + " of size " + (size) + " bytes");
                con.commit();
                i++;
            }
            for (int i = 1; i < nblocks; i += 5) {
                int size = blockSize * i;
                pstin.clearParameters();
                pstin.setInt(1, i);
                rs = pstin.executeQuery();
                if (rs.next()) {
                    instream = rs.getBinaryStream(1);
                    DataInputStream din = new DataInputStream(instream);
                    din.readFully(bin, 0, size);
                    for (int j = 0; j < size; j++) {
                        Assert.assertEquals("Byte Missmatch record " + i + " offset " + j, b[j], bin[j]);
                    }
                    log.info("Checked Record " + i + " of size " + size + " bytes");
                    din.close();
                    instream.close();
                    rs.close();
                    i++;
                } else {
                    Assert.assertEquals("Didnt get any record at " + i, true, false);
                }
                con.commit();
            }
        } finally {
            try {
                pstin.close();
            } catch (SQLException e) {

            }
            try {
                pstout.close();
            } catch (SQLException e) {

            }
            try {
                instream.close();
            } catch (Exception ex) {
            }
            try {
                rs.close();
            } catch (Exception ex) {
            }

        }
    } catch (Exception ex) {
        log.error("Failed ", ex);
    }

}

From source file:fr.noop.subtitle.stl.StlParser.java

private String readString(DataInputStream dis, int length) throws IOException {
    byte[] bytes = new byte[length];
    dis.readFully(bytes, 0, length);

    // Remove spaces at start and end of the string
    return StringUtils.strip(new String(bytes));
}

From source file:fr.noop.subtitle.stl.StlParser.java

private String readString(DataInputStream dis, int length, String charset) throws IOException {
    byte[] bytes = new byte[length];
    dis.readFully(bytes, 0, length);

    // Remove spaces at start and end of the string
    return StringUtils.strip(new String(bytes, charset));
}

From source file:hd3gtv.embddb.network.DataBlock.java

/**
 * Import mode//from ww w.  ja v  a  2 s . co m
 */
DataBlock(Protocol protocol, byte[] request_raw_datas) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace("Get raw datas" + Hexview.LINESEPARATOR + Hexview.tracelog(request_raw_datas));
    }

    ByteArrayInputStream inputstream_client_request = new ByteArrayInputStream(request_raw_datas);

    DataInputStream dis = new DataInputStream(inputstream_client_request);

    byte[] app_socket_header_tag = new byte[Protocol.APP_SOCKET_HEADER_TAG.length];
    dis.readFully(app_socket_header_tag, 0, Protocol.APP_SOCKET_HEADER_TAG.length);

    if (Arrays.equals(Protocol.APP_SOCKET_HEADER_TAG, app_socket_header_tag) == false) {
        throw new IOException("Protocol error with app_socket_header_tag");
    }

    int version = dis.readInt();
    if (version != Protocol.VERSION) {
        throw new IOException(
                "Protocol error with version, this = " + Protocol.VERSION + " and dest = " + version);
    }

    byte tag = dis.readByte();
    if (tag != 0) {
        throw new IOException("Protocol error, can't found request_name raw datas");
    }

    int size = dis.readInt();
    if (size < 1) {
        throw new IOException(
                "Protocol error, can't found request_name raw datas size is too short (" + size + ")");
    }

    byte[] request_name_raw = new byte[size];
    dis.read(request_name_raw);
    request_name = new String(request_name_raw, Protocol.UTF8);

    tag = dis.readByte();
    if (tag != 1) {
        throw new IOException("Protocol error, can't found zip raw datas");
    }

    entries = new ArrayList<>(1);

    ZipInputStream request_zip = new ZipInputStream(dis);

    ZipEntry entry;
    while ((entry = request_zip.getNextEntry()) != null) {
        entries.add(new RequestEntry(entry, request_zip));
    }
    request_zip.close();
}

From source file:com.l2jfree.gameserver.util.JarClassLoader.java

private byte[] loadClassData(String name) throws IOException {
    byte[] classData = null;
    for (String jarFile : _jars) {
        ZipFile zipFile = null;//from w  ww .j a v a  2 s.  com
        DataInputStream zipStream = null;

        try {
            File file = new File(jarFile);
            zipFile = new ZipFile(file);
            String fileName = name.replace('.', '/') + ".class";
            ZipEntry entry = zipFile.getEntry(fileName);
            if (entry == null)
                continue;
            classData = new byte[(int) entry.getSize()];
            zipStream = new DataInputStream(zipFile.getInputStream(entry));
            zipStream.readFully(classData, 0, (int) entry.getSize());
            break;
        } catch (IOException e) {
            _log.warn(jarFile + ":", e);
            continue;
        } finally {
            try {
                if (zipFile != null)
                    zipFile.close();
            } catch (Exception e) {
                _log.warn("", e);
            }
            try {
                if (zipStream != null)
                    zipStream.close();
            } catch (Exception e) {
                _log.warn("", e);
            }
        }
    }
    if (classData == null)
        throw new IOException("class not found in " + _jars);
    return classData;
}

From source file:runtime.daemon.ProcessLauncher.java

private String getStringFromInputStream(InputStream is) {

    DataInputStream in = new DataInputStream(is);
    int len = 0;// w  w w.j av  a 2s  . co  m
    try {
        len = in.readInt();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (DEBUG && logger.isDebugEnabled()) {
        logger.debug("Ticket length  " + len);
    }
    byte[] xml = new byte[len];
    try {

        in.readFully(xml, 0, len);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new String(xml);
}

From source file:fr.noop.subtitle.stl.StlParser.java

private StlGsi readGsi(DataInputStream dis) throws IOException, InvalidTimeRangeException {
    // Read and extract metadata from GSI block
    // GSI block is 1024 bytes long
    StlGsi gsi = new StlGsi();

    // Read Code Page Number (CPN)
    byte[] cpnBytes = new byte[3];
    dis.readFully(cpnBytes, 0, 3);
    int cpn = cpnBytes[0] << 16 | cpnBytes[1] << 8 | cpnBytes[2];
    gsi.setCpn(StlGsi.Cpn.getEnum(cpn));

    // Read Disk Format Code (DFC)
    gsi.setDfc(StlGsi.Dfc.getEnum(this.readString(dis, 8)));

    // Read Display Standard Code (DSC)
    gsi.setDsc(StlGsi.Dsc.getEnum(dis.readUnsignedByte()));

    // Read Character Code Table number (CCT)
    gsi.setCct(StlGsi.Cct.getEnum(Short.reverseBytes(dis.readShort())));

    // Read Character Language Code (LC)
    gsi.setLc(Short.reverseBytes(dis.readShort()));

    // Read Original Programme Title (OPT)
    gsi.setOpt(this.readString(dis, 32));

    // Read Original Programme Title (OET)
    gsi.setOet(this.readString(dis, 32));

    // Read Translated Programme Title (TPT)
    gsi.setTpt(this.readString(dis, 32));

    // Read translated Episode Title (TET)
    gsi.setTet(this.readString(dis, 32));

    // Read Translator's Name (TN)
    gsi.setTn(this.readString(dis, 32));

    // Read Translator's Contact Details (TCD)
    gsi.setTcd(this.readString(dis, 32));

    // Read Subtitle List Reference Code (SLR)
    gsi.setSlr(this.readString(dis, 16));

    // Read Creation Date (CD)
    gsi.setCd(this.readDate(this.readString(dis, 6)));

    // Read Revision Date (RD)
    gsi.setRd(this.readDate(this.readString(dis, 6)));

    // Read Revision number RN
    gsi.setRn(Short.reverseBytes(dis.readShort()));

    // Read Total Number of Text and Timing Information (TTI) blocks (TNB)
    gsi.setTnb(Integer.parseInt(this.readString(dis, 5)));

    // Read Total Number of Subtitles (TNS)
    gsi.setTns(Integer.parseInt(this.readString(dis, 5)));

    // Read Total Number of Subtitle Groups (TNG)
    dis.skipBytes(3);/*from  w  w w .  ja va 2  s. c om*/

    // Read Maximum Number of Displayable Characters in any text row (MNC)
    gsi.setMnc(Integer.parseInt(this.readString(dis, 2)));

    // Read Maximum Number of Displayable Rows (MNR)
    gsi.setMnr(Integer.parseInt(this.readString(dis, 2)));

    // Read Time Code: Status (TCS)
    gsi.setTcs((short) dis.readUnsignedByte());

    // Read Time Code: Start-of-Programme (TCP)
    gsi.setTcp(this.readTimeCode(this.readString(dis, 8), gsi.getDfc().getFrameRate()));

    // Read Time Code: First In-Cue (TCF)
    gsi.setTcf(this.readTimeCode(this.readString(dis, 8), gsi.getDfc().getFrameRate()));

    // Read Total Number of Disks (TND)
    gsi.setTnd((short) dis.readUnsignedByte());

    // Read Disk Sequence Number (DSN)
    gsi.setDsn((short) dis.readUnsignedByte());

    // Read Country of Origin (CO)
    gsi.setCo(this.readString(dis, 3));

    // Read Publisher (PUB)
    gsi.setPub(this.readString(dis, 32));

    // Read Editor's Name (EN)
    gsi.setEn(this.readString(dis, 32));

    // Read Editor's Contact Details (ECD)
    gsi.setEcd(this.readString(dis, 32));

    // Spare Bytes
    dis.skipBytes(75);

    // Read User-Defined Area (UDA)
    gsi.setUda(this.readString(dis, 576));

    return gsi;
}