Example usage for java.io DataInputStream readByte

List of usage examples for java.io DataInputStream readByte

Introduction

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

Prototype

public final byte readByte() throws IOException 

Source Link

Document

See the general contract of the readByte method of DataInput.

Usage

From source file:edu.cornell.med.icb.goby.alignments.TestSkipTo.java

private void read10(InputStream is, long startOffset) throws IOException {
    DataInputStream dis = new DataInputStream(is);
    for (int i = 0; i < 10; i++) {
        final byte b = dis.readByte();
        System.out.println(Byte.toString(b));
        if (i == 9) {
            switch ((int) startOffset) {
            case 0:
                assertEquals(2, b);/*from w w w .j  a va 2s  .c o m*/
                break;
            case 2:
                assertEquals(-59, b);
                break;
            case 9:
                assertEquals(0, b);
                break;

            }
        }
    }
}

From source file:de.hybris.platform.cuppytrail.impl.DefaultSecureTokenService.java

private void skipPadding(final DataInputStream dataInputStream) throws IOException {
    final int firstByte = dataInputStream.readUnsignedByte();

    // Find number of additional bytes to skip (stored in the bottom 3 bits)
    final int length = firstByte & 0x0007;

    for (int i = 0; i < length; i++) {
        dataInputStream.readByte();
    }/*from  w w  w  .  j a v a 2 s  .com*/
}

From source file:be.ibridge.kettle.job.entry.filecompare.JobEntryFileCompare.java

/**
 * Check whether 2 files have the same contents.
 *
 * @param file1 first file to compare/*w ww  . java2  s . c om*/
 * @param file2 second file to compare
 * @return true if files are equal, false if they are not
 *
 * @throws IOException upon IO problems
 */
protected boolean equalFileContents(FileObject file1, FileObject file2) throws IOException {
    // Really read the contents and do comparisons

    DataInputStream in1 = new DataInputStream(
            new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file1))));
    DataInputStream in2 = new DataInputStream(
            new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file2))));

    char ch1, ch2;
    while (in1.available() != 0 && in2.available() != 0) {
        ch1 = (char) in1.readByte();
        ch2 = (char) in2.readByte();
        if (ch1 != ch2)
            return false;
    }
    if (in1.available() != in2.available()) {
        return false;
    } else {
        return true;
    }
}

From source file:ubic.gemma.core.analysis.preprocess.batcheffects.AffyScanDateExtractor.java

/**
 * @return A UNICODE string. A string object is stored as an INT (to store the string length) followed by the WCHAR array
 * (to store the string contents).//w w  w . j a  v  a  2 s  .  c  om
 * (http://media.affymetrix.com/support/developer/powertools/changelog/gcos-agcc/generic.html)
 */
private String readUnicodeString(DataInputStream str) throws IOException {
    int fieldLength = this.readIntBigEndian(str);

    // log.info( fieldLength );

    byte[] buf = new byte[fieldLength * 2];

    // StringBuilder buf = new StringBuilder();
    for (int i = 0; i < fieldLength * 2; i++) {
        buf[i] = str.readByte();
    }

    return new String(buf, "UTF-16");
}

From source file:org.apache.hadoop.hbase.migration.nineteen.regionserver.HStoreFile.java

/** 
 * Reads in an info file/* ww w. java 2s . c  o m*/
 *
 * @param filesystem file system
 * @return The sequence id contained in the info file
 * @throws IOException
 */
public long loadInfo(final FileSystem filesystem) throws IOException {
    Path p = null;
    if (isReference()) {
        p = getInfoFilePath(reference.getEncodedRegionName(), this.reference.getFileId());
    } else {
        p = getInfoFilePath();
    }
    long length = filesystem.getFileStatus(p).getLen();
    boolean hasMoreThanSeqNum = length > (Byte.SIZE + Bytes.SIZEOF_LONG);
    DataInputStream in = new DataInputStream(filesystem.open(p));
    try {
        byte flag = in.readByte();
        if (flag == INFO_SEQ_NUM) {
            if (hasMoreThanSeqNum) {
                flag = in.readByte();
                if (flag == MAJOR_COMPACTION) {
                    this.majorCompaction = in.readBoolean();
                }
            }
            return in.readLong();
        }
        throw new IOException("Cannot process log file: " + p);
    } finally {
        in.close();
    }
}

From source file:org.motechproject.mobile.web.OXDFormDownloadServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* w ww.  j  av  a2s.  com*/
 *
 * @param request  servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException      if an I/O error occurs
 */
@RequestMapping(method = RequestMethod.POST)
public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // Get our raw input and output streams
    InputStream input = request.getInputStream();
    OutputStream output = response.getOutputStream();

    // Wrap the streams for compression
    ZOutputStream zOutput = new ZOutputStream(output, JZlib.Z_BEST_COMPRESSION);

    // Wrap the streams so we can use logical types
    DataInputStream dataInput = new DataInputStream(input);
    DataOutputStream dataOutput = new DataOutputStream(zOutput);

    try {

        // Read the common submission data from mobile phone
        String name = dataInput.readUTF();
        String password = dataInput.readUTF();
        String serializer = dataInput.readUTF();
        String locale = dataInput.readUTF();

        byte action = dataInput.readByte();

        // TODO: add authentication, possible M6 enhancement

        log.info("downloading: name=" + name + ", password=" + password + ", serializer=" + serializer
                + ", locale=" + locale + ", action=" + action);

        EpihandyXformSerializer serObj = new EpihandyXformSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // Perform the action specified by the mobile phone
        try {
            if (action == ACTION_DOWNLOAD_STUDY_LIST) {
                serObj.serializeStudies(baos, studyService.getStudies());
            } else if (action == ACTION_DOWNLOAD_USERS_AND_FORMS) {

                serObj.serializeUsers(baos, userService.getUsers());

                int studyId = dataInput.readInt();
                String studyName = studyService.getStudyName(studyId);
                List<String> studyForms = formService.getStudyForms(studyId);

                serObj.serializeForms(baos, studyForms, studyId, studyName);

            }
        } catch (Exception e) {
            dataOutput.writeByte(RESPONSE_ERROR);
            throw new ServletException("failed to serialize data", e);
        }

        // Write out successful upload response
        dataOutput.writeByte(RESPONSE_SUCCESS);
        dataOutput.write(baos.toByteArray());
        response.setStatus(HttpServletResponse.SC_OK);

    } finally {
        // Should always do this
        dataOutput.flush();
        zOutput.finish();
        response.flushBuffer();
    }
}

From source file:ubic.gemma.core.analysis.preprocess.batcheffects.AffyScanDateExtractor.java

/**
 * The data is stored as a int, then the array of bytes.
 */// w w  w.  j  a va2 s  .  c om
private byte[] readBytes(DataInputStream str) throws IOException {
    int fieldLength = this.readIntBigEndian(str);

    byte[] result = new byte[fieldLength];
    for (int i = 0; i < fieldLength; i++) {
        if (str.available() == 0)
            throw new IOException("Reached end of file without string end");
        result[i] = str.readByte();
    }

    return result;
}

From source file:gobblin.writer.SimpleDataWriterTest.java

/**
 * Prepend the size to each record without delimiting the record. Each record
 * should be prepended by the size of that record and the bytes written should
 * include the prepended bytes./*w  ww  .  ja  v  a  2 s .c om*/
 */
@Test
public void testPrependSizeWithoutDelimiter() throws IOException {
    properties.setProp(ConfigurationKeys.SIMPLE_WRITER_PREPEND_SIZE, true);
    properties.setProp(ConfigurationKeys.SIMPLE_WRITER_DELIMITER, "");
    SimpleDataWriter writer = buildSimpleDataWriter();
    byte[] rec1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    byte[] rec2 = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
    byte[] rec3 = { 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45 };
    byte[][] records = { rec1, rec2, rec3 };

    writer.write(rec1);
    writer.write(rec2);
    writer.write(rec3);

    writer.close();
    writer.commit();

    Assert.assertEquals(writer.recordsWritten(), 3);
    Assert.assertEquals(writer.bytesWritten(), rec1.length + rec2.length + rec3.length + (Long.SIZE / 8 * 3));

    File outputFile = new File(writer.getOutputFilePath());
    DataInputStream dis = new DataInputStream(new FileInputStream(outputFile));
    for (int i = 0; i < 3; i++) {
        long size = dis.readLong();
        Assert.assertEquals(size, records[i].length);
        for (int j = 0; j < size; j++) {
            Assert.assertEquals(dis.readByte(), records[i][j]);
        }
    }
}

From source file:ubic.gemma.core.analysis.preprocess.batcheffects.AffyScanDateExtractor.java

/**
 * @return A 1 byte character string. A string object is stored as an INT (to store the string length) followed by the CHAR
 * array (to store the string contents).
 *//*from   ww w  .j  a  v a  2 s  .c o m*/
private String readString(DataInputStream str) throws IOException {
    int fieldLength = this.readIntBigEndian(str);
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < fieldLength; i++) {
        if (str.available() == 0)
            throw new IOException("Reached end of file without string end");
        buf.append(new String(new byte[] { str.readByte() }));
    }
    return buf.toString();
}

From source file:gobblin.writer.SimpleDataWriterTest.java

/**
 * Prepend the size to each record and delimit the record. Each record
 * should be prepended by the size of that record and the bytes written should
 * include the prepended bytes.//from  ww  w . ja v  a 2 s .  c  o  m
 */
@Test
public void testPrependSizeWithDelimiter() throws IOException {
    properties.setProp(ConfigurationKeys.SIMPLE_WRITER_PREPEND_SIZE, true);
    SimpleDataWriter writer = buildSimpleDataWriter();
    byte[] rec1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    byte[] rec2 = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
    byte[] rec3 = { 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45 };
    byte[][] records = { rec1, rec2, rec3 };

    writer.write(rec1);
    writer.write(rec2);
    writer.write(rec3);

    writer.close();
    writer.commit();

    Assert.assertEquals(writer.recordsWritten(), 3);
    Assert.assertEquals(writer.bytesWritten(),
            rec1.length + rec2.length + rec3.length + (Long.SIZE / 8 * 3) + 3);

    File outputFile = new File(writer.getOutputFilePath());
    DataInputStream dis = new DataInputStream(new FileInputStream(outputFile));
    for (int i = 0; i < 3; i++) {
        long size = dis.readLong();
        Assert.assertEquals(size, records[i].length + 1);
        for (int j = 0; j < size - 1; j++) {
            Assert.assertEquals(dis.readByte(), records[i][j]);
        }
        Assert.assertEquals(dis.readByte(), '\n');
    }
}