Example usage for java.io DataInputStream readInt

List of usage examples for java.io DataInputStream readInt

Introduction

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

Prototype

public final int readInt() throws IOException 

Source Link

Document

See the general contract of the readInt method of DataInput.

Usage

From source file:org.apache.xmlgraphics.image.codec.png.PNGRed.java

private static PNGChunk readChunk(final DataInputStream distream) {
    try {//from ww w.j a v a  2s .com
        final int length = distream.readInt();
        final int type = distream.readInt();
        final byte[] data = new byte[length];
        distream.readFully(data);
        final int crc = distream.readInt();

        return new PNGChunk(length, type, data, crc);
    } catch (final Exception e) {
        log.error("Exception", e);
        return null;
    }
}

From source file:org.apache.tajo.ws.rs.resources.TestQueryResultResource.java

@Test
public void testGetQueryResultSet() throws Exception {
    String sessionId = generateNewSessionAndGetId();
    URI queryIdURI = sendNewQueryResquest(sessionId, "select * from lineitem");
    URI queryResultURI = new URI(queryIdURI + "/result");

    GetQueryResultDataResponse response = restClient.target(queryResultURI).request()
            .header(tajoSessionIdHeaderName, sessionId)
            .get(new GenericType<GetQueryResultDataResponse>(GetQueryResultDataResponse.class));

    assertNotNull(response);//from www .  ja  v a  2  s  .  c  o m
    assertNotNull(response.getResultCode());
    assertTrue(ErrorUtil.isOk(response.getResultCode()));
    assertNotNull(response.getSchema());
    assertEquals(16, response.getSchema().getRootColumns().size());
    assertNotNull(response.getResultset());
    assertTrue(response.getResultset().getId() != 0);
    assertNotNull(response.getResultset().getLink());

    URI queryResultSetURI = response.getResultset().getLink();

    Response queryResultSetResponse = restClient.target(queryResultSetURI).queryParam("count", 100).request()
            .header(tajoSessionIdHeaderName, sessionId).get();

    assertNotNull(queryResultSetResponse);
    String tajoDigest = queryResultSetResponse.getHeaderString(tajoDigestHeaderName);
    assertTrue(tajoDigest != null && !tajoDigest.isEmpty());

    DataInputStream queryResultSetInputStream = new DataInputStream(
            new BufferedInputStream(queryResultSetResponse.readEntity(InputStream.class)));

    assertNotNull(queryResultSetInputStream);

    boolean isFinished = false;
    List<Tuple> tupleList = TUtil.newList();
    RowStoreUtil.RowStoreDecoder decoder = RowStoreUtil.createDecoder(response.getSchema());
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    while (!isFinished) {
        try {
            int length = queryResultSetInputStream.readInt();
            byte[] dataByteArray = new byte[length];
            int readBytes = queryResultSetInputStream.read(dataByteArray);

            assertEquals(length, readBytes);

            tupleList.add(decoder.toTuple(dataByteArray));
            messageDigest.update(dataByteArray);
        } catch (EOFException eof) {
            isFinished = true;
        }
    }

    assertEquals(5, tupleList.size());
    assertEquals(tajoDigest, Base64.encodeBase64String(messageDigest.digest()));

    for (Tuple aTuple : tupleList) {
        assertTrue(aTuple.getInt4(response.getSchema().getColumnId("l_orderkey")) > 0);
    }
}

From source file:org.pentaho.di.trans.steps.blockingstep.BlockingStep.java

private Object[] getBuffer() {
    Object[] retval;//from w  w w . j a  v a2  s  . c  o m

    // Open all files at once and read one row from each file...
    if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
        if (log.isBasic()) {
            logBasic(BaseMessages.getString(PKG, "BlockingStep.Log.Openfiles"));
        }

        try {
            FileObject fileObject = data.files.get(0);
            String filename = KettleVFS.getFilename(fileObject);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "BlockingStep.Log.Openfilename1") + filename
                        + BaseMessages.getString(PKG, "BlockingStep.Log.Openfilename2"));
            }
            InputStream fi = KettleVFS.getInputStream(fileObject);
            DataInputStream di;
            data.fis.add(fi);
            if (meta.getCompress()) {
                GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi));
                di = new DataInputStream(gzfi);
                data.gzis.add(gzfi);
            } else {
                di = new DataInputStream(fi);
            }
            data.dis.add(di);

            // How long is the buffer?
            int buffersize = di.readInt();

            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "BlockingStep.Log.BufferSize1") + filename
                        + BaseMessages.getString(PKG, "BlockingStep.Log.BufferSize2") + buffersize + " "
                        + BaseMessages.getString(PKG, "BlockingStep.Log.BufferSize3"));
            }

            if (buffersize > 0) {
                // Read a row from temp-file
                data.rowbuffer.add(data.outputRowMeta.readData(di));
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "BlockingStepMeta.ErrorReadingFile") + e.toString());
            logError(Const.getStackTracker(e));
        }
    }

    if (data.files.size() == 0) {
        if (data.buffer.size() > 0) {
            retval = data.buffer.get(0);
            data.buffer.remove(0);
        } else {
            retval = null;
        }
    } else {
        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            retval = data.rowbuffer.get(0);

            data.rowbuffer.remove(0);

            // now get another
            FileObject file = data.files.get(0);
            DataInputStream di = data.dis.get(0);
            InputStream fi = data.fis.get(0);
            GZIPInputStream gzfi = (meta.getCompress()) ? data.gzis.get(0) : null;

            try {
                data.rowbuffer.add(0, data.outputRowMeta.readData(di));
            } catch (SocketTimeoutException e) {
                logError(BaseMessages.getString(PKG, "System.Log.UnexpectedError") + " : " + e.toString());
                logError(Const.getStackTracker(e));
                setErrors(1);
                stopAll();
            } catch (KettleFileException fe) {
                // empty file or EOF mostly
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null) {
                        gzfi.close();
                    }
                    file.delete();
                } catch (IOException e) {
                    logError(
                            BaseMessages.getString(PKG, "BlockingStepMeta.UnableDeleteFile") + file.toString());
                    setErrors(1);
                    stopAll();
                    return null;
                }

                data.files.remove(0);
                data.dis.remove(0);
                data.fis.remove(0);
                if (gzfi != null) {
                    data.gzis.remove(0);
                }
            }
        }
    }
    return retval;
}

From source file:com.igormaznitsa.jhexed.hexmap.HexFieldLayer.java

public HexFieldLayer(final InputStream in) throws IOException {
    final DataInputStream din = in instanceof DataInputStream ? (DataInputStream) in : new DataInputStream(in);
    this.name = din.readUTF();
    this.comments = din.readUTF();

    final int valuesNum = din.readShort() & 0xFFFF;
    for (int i = 0; i < valuesNum; i++) {
        final HexFieldValue value = HexFieldValue.readValue(in);
        value.setIndex(i);//w w  w .  jav  a2  s . c  o m
        this.values.add(value);
    }

    this.columns = din.readInt();
    this.rows = din.readInt();
    this.visible = din.readBoolean();

    final byte[] packedLayerData = new byte[din.readInt()];
    IOUtils.readFully(din, packedLayerData);
    this.array = Utils.unpackArray(packedLayerData);

    if (this.array.length != (this.columns * this.rows))
        throw new IOException("Wrong field size");
}

From source file:org.apache.tajo.ws.rs.resources.TestQueryResultResource.java

@Test
public void testGetQueryResultSetWithDefaultCount() throws Exception {
    String sessionId = generateNewSessionAndGetId();
    URI queryIdURI = sendNewQueryResquest(sessionId, "select * from lineitem");
    URI queryResultURI = new URI(queryIdURI + "/result");

    GetQueryResultDataResponse response = restClient.target(queryResultURI).request()
            .header(tajoSessionIdHeaderName, sessionId)
            .get(new GenericType<GetQueryResultDataResponse>(GetQueryResultDataResponse.class));

    assertNotNull(response);//from   ww w.java 2 s .  c o  m
    assertNotNull(response.getResultCode());
    assertTrue(isOk(response.getResultCode()));
    assertNotNull(response.getSchema());
    assertEquals(16, response.getSchema().getRootColumns().size());
    assertNotNull(response.getResultset());
    assertTrue(response.getResultset().getId() != 0);
    assertNotNull(response.getResultset().getLink());

    URI queryResultSetURI = response.getResultset().getLink();

    Response queryResultSetResponse = restClient.target(queryResultSetURI).request()
            .header(tajoSessionIdHeaderName, sessionId).get();

    assertNotNull(queryResultSetResponse);
    String tajoDigest = queryResultSetResponse.getHeaderString(tajoDigestHeaderName);
    int offset = Integer.valueOf(queryResultSetResponse.getHeaderString(tajoOffsetHeaderName));
    int count = Integer.valueOf(queryResultSetResponse.getHeaderString(tajoCountHeaderName));
    boolean eos = Boolean.valueOf(queryResultSetResponse.getHeaderString(tajoEOSHeaderName));

    assertTrue(tajoDigest != null && !tajoDigest.isEmpty());
    assertTrue(eos);
    assertEquals(0, offset);
    assertEquals(5, count);

    DataInputStream queryResultSetInputStream = new DataInputStream(
            new BufferedInputStream(queryResultSetResponse.readEntity(InputStream.class)));

    assertNotNull(queryResultSetInputStream);

    boolean isFinished = false;
    List<Tuple> tupleList = TUtil.newList();
    RowStoreUtil.RowStoreDecoder decoder = RowStoreUtil.createDecoder(response.getSchema());
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    while (!isFinished) {
        try {
            int length = queryResultSetInputStream.readInt();
            byte[] dataByteArray = new byte[length];
            int readBytes = queryResultSetInputStream.read(dataByteArray);

            assertEquals(length, readBytes);

            tupleList.add(decoder.toTuple(dataByteArray));
            messageDigest.update(dataByteArray);
        } catch (EOFException eof) {
            isFinished = true;
        }
    }

    assertEquals(5, tupleList.size());
    assertEquals(tajoDigest, Base64.encodeBase64String(messageDigest.digest()));

    for (Tuple aTuple : tupleList) {
        assertTrue(aTuple.getInt4(response.getSchema().getColumnId("l_orderkey")) > 0);
    }
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGRed.java

private static String getChunkType(final DataInputStream distream) {
    try {/*w  w w  .  ja  v a 2 s. c o m*/
        distream.mark(8);
        /* int length = */distream.readInt();
        final int type = distream.readInt();
        distream.reset();

        final String typeString = "" + (char) (type >> 24 & 0xff) + (char) (type >> 16 & 0xff)
                + (char) (type >> 8 & 0xff) + (char) (type & 0xff);
        return typeString;
    } catch (final Exception e) {
        log.error("Exception", e);
        return null;
    }
}

From source file:com.facebook.infrastructure.db.SuperColumn.java

public IColumn deserialize(DataInputStream dis, IFilter filter) throws IOException {
    if (dis.available() == 0)
        return null;

    IColumn superColumn = defreezeSuperColumn(dis);
    superColumn = filter.filter(superColumn, dis);
    if (superColumn != null) {
        if (!superColumn.isMarkedForDelete())
            fillSuperColumn(superColumn, dis);
        return superColumn;
    } else {/* w  w w .j a  v a 2s .c o m*/
        /* read the number of columns stored */
        dis.readInt();
        /* read the size of all columns to skip */
        int size = dis.readInt();
        dis.skip(size);
        return null;
    }
}

From source file:runtime.starter.MPJRun.java

private Integer[] getNextAvialablePorts(String machineName) {

    Integer[] ports = new Integer[2];

    Socket portClient = null;/*from  ww w  . j  av  a 2s  .c  o  m*/
    try {
        portClient = new Socket(machineName, portManagerPort);
        OutputStream outToServer = portClient.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeInt(1);
        out.flush();
        DataInputStream din = new DataInputStream(portClient.getInputStream());
        ports[0] = din.readInt();
        ports[1] = din.readInt();
        out.writeInt(2);
        out.flush();

    } catch (IOException e) {
        System.out.println("Cannot connect to the daemon " + "at machine <" + machineName + "> and port <"
                + portManagerPort + ">." + "Please make sure that the machine is reachable "
                + "and portmanager is running");
    } finally {
        try {
            if (!portClient.isClosed())
                portClient.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
    return ports;
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Checks a <code>PropertyState</code> from the data input stream.
 *
 * @param in the input stream/*ww w . j a v  a2  s.c o m*/
 * @return <code>true</code> if the data is valid;
 *         <code>false</code> otherwise.
 */
public boolean checkPropertyState(DataInputStream in) {
    int type;
    try {
        type = in.readInt();
        short modCount = (short) ((type >> 16) | 0xffff);
        type &= 0xffff;
        log.debug("  PropertyType: " + PropertyType.nameFromValue(type));
        log.debug("  ModCount: " + modCount);
    } catch (IOException e) {
        log.error("Error while reading property type: " + e);
        return false;
    }
    try {
        boolean isMV = in.readBoolean();
        log.debug("  MultiValued: " + isMV);
    } catch (IOException e) {
        log.error("Error while reading multivalued: " + e);
        return false;
    }
    try {
        String defintionId = in.readUTF();
        log.debug("  DefinitionId: " + defintionId);
    } catch (IOException e) {
        log.error("Error while reading definition id: " + e);
        return false;
    }

    int count;
    try {
        count = in.readInt();
        log.debug("  num values: " + count);
    } catch (IOException e) {
        log.error("Error while reading number of values: " + e);
        return false;
    }
    for (int i = 0; i < count; i++) {
        switch (type) {
        case PropertyType.BINARY:
            int size;
            try {
                size = in.readInt();
                log.debug("  binary size: " + size);
            } catch (IOException e) {
                log.error("Error while reading size of binary: " + e);
                return false;
            }
            if (size == BINARY_IN_DATA_STORE) {
                try {
                    String s = in.readUTF();
                    // truncate log output
                    if (s.length() > 80) {
                        s = s.substring(80) + "...";
                    }
                    log.debug("  global data store id: " + s);
                } catch (IOException e) {
                    log.error("Error while reading blob id: " + e);
                    return false;
                }
            } else if (size == BINARY_IN_BLOB_STORE) {
                try {
                    String s = in.readUTF();
                    log.debug("  blobid: " + s);
                } catch (IOException e) {
                    log.error("Error while reading blob id: " + e);
                    return false;
                }
            } else {
                // short values into memory
                byte[] data = new byte[size];
                try {
                    in.readFully(data);
                    log.debug("  binary: " + data.length + " bytes");
                } catch (IOException e) {
                    log.error("Error while reading inlined binary: " + e);
                    return false;
                }
            }
            break;
        case PropertyType.DOUBLE:
            try {
                double d = in.readDouble();
                log.debug("  double: " + d);
            } catch (IOException e) {
                log.error("Error while reading double value: " + e);
                return false;
            }
            break;
        case PropertyType.LONG:
            try {
                double l = in.readLong();
                log.debug("  long: " + l);
            } catch (IOException e) {
                log.error("Error while reading long value: " + e);
                return false;
            }
            break;
        case PropertyType.BOOLEAN:
            try {
                boolean b = in.readBoolean();
                log.debug("  boolean: " + b);
            } catch (IOException e) {
                log.error("Error while reading boolean value: " + e);
                return false;
            }
            break;
        case PropertyType.NAME:
            try {
                Name name = readQName(in);
                log.debug("  name: " + name);
            } catch (IOException e) {
                log.error("Error while reading name value: " + e);
                return false;
            }
            break;
        case PropertyType.REFERENCE:
            try {
                UUID uuid = readUUID(in);
                log.debug("  reference: " + uuid);
            } catch (IOException e) {
                log.error("Error while reading reference value: " + e);
                return false;
            }
            break;
        default:
            // because writeUTF(String) has a size limit of 64k,
            // Strings are serialized as <length><byte[]>
            int len;
            try {
                len = in.readInt();
                log.debug("  size of string value: " + len);
            } catch (IOException e) {
                log.error("Error while reading size of string value: " + e);
                return false;
            }
            try {
                byte[] bytes = new byte[len];
                in.readFully(bytes);
                String s = new String(bytes, "UTF-8");
                // truncate log output
                if (s.length() > 80) {
                    s = s.substring(80) + "...";
                }
                log.debug("  string: " + s);
            } catch (IOException e) {
                log.error("Error while reading string value: " + e);
                return false;
            }
        }
    }
    return true;
}

From source file:uk.ac.ox.webauth.FilterWorker.java

/**
 * Try to decrypt a token, first with the key given, and then with any key
 * we know about, throwing an exception if we don't succeed.
 * @param   base64data  The Base64 encoded data of a token.
 * @param   type        What type of token this is to validate it as.
 * @return  A validated token./*from  w w  w  .j ava2  s.  co  m*/
 * @throws  ServletException    if it wasn't possible to encrypt or validate
 *          the token.
 */
private Token decrypt(String base64data, String type) throws ServletException {
    Token token = null;
    byte[] tokenData = base64decode(base64data);
    // first try with the given key
    try {
        token = new Token(tokenData, privateKey.key());
        if (logger.debug()) {
            debug("Decrypted '" + type + "' type token with key " + privateKey.kn() + ".");
        }
    } catch (GeneralSecurityException gse) {
        if (logger.debug()) {
            debug("Could not decrypt " + type + " type token with supplied key " + privateKey.kn() + ".");
        }
    }
    // otherwise try with every other private key we know of
    if (token == null) {
        List<WebauthKey> suitable = null;
        try {
            DataInputStream din = new DataInputStream(new ByteArrayInputStream(tokenData));
            suitable = privateKeyManager.suitableKeys(din.readInt());
        } catch (IOException ioe) {
            // should never happen since it's a ByteArrayInputStream
            throw new ServletException(ioe);
        }
        suitable.remove(privateKey);
        for (WebauthKey wk : suitable) {
            try {
                token = new Token(tokenData, wk.key());
            } catch (GeneralSecurityException gse) {
                if (logger.debug()) {
                    debug("Could not decrypt '" + type + "' type token with key " + wk.kn() + ".");
                }
            }
            if (token != null) {
                if (logger.debug()) {
                    debug("Decrypted '" + type + "' type token with key " + wk.kn() + ".");
                }
                break;
            }
        }
    }
    if (token == null) {
        throw new ServletException("Could not decrypt '" + type + "' type token with any key.");
    }

    // now validate the token
    if (!Validator.validate(type, token, logger)) {
        throw new ServletException("'" + type + "' token does not validate.");
    }
    return token;
}