Example usage for java.io DataInputStream reset

List of usage examples for java.io DataInputStream reset

Introduction

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

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

Repositions this stream to the position at the time the mark method was last called on this input stream.

Usage

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

/**
 * Returns the PNG chunk type, a four letter case sensitive ASCII type/name.
 * @param distream the input stream//  w ww.j  a v a 2  s .co m
 * @return a four letter case sensitive ASCII type/name
 */
public static String getChunkType(DataInputStream distream) {
    try {
        distream.mark(8);
        /* int length = */distream.readInt();
        int type = distream.readInt();
        distream.reset();

        return typeIntToString(type);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

private static String getChunkType(final DataInputStream distream) {
    try {/*from  w  w w.  j a va2  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:org.mule.transport.comm.protocols.LengthProtocol.java

public Object read(InputStream is) throws IOException {
    // original comments indicated that we need to use read(byte[]) rather than readInt()
    // to avoid socket timeouts - don't understand, but don't want to risk change.

    // first read the data necessary to know the length of the payload
    DataInputStream dis = new DataInputStream(is);
    dis.mark(SIZE_INT);/* w w  w . j  av  a  2 s. c o m*/
    // this pulls through SIZE_INT bytes
    if (null == super.read(dis, SIZE_INT)) {
        return null; // eof
    }

    // reset and read the integer
    dis.reset();
    int length = dis.readInt();
    if (logger.isDebugEnabled()) {
        logger.debug("length: " + length);
    }

    if (length < 0 || (getMaxMessageLength() > 0 && length > getMaxMessageLength())) {
        //            throw new IOException("Length " + length + " exceeds limit: " + getMaxMessageLength());
        System.out.println("Length " + length + " exceeds limit: " + getMaxMessageLength());
    }

    // finally read the rest of the data
    if (length > 0) {
        byte[] buffer = new byte[0];
        dis.readFully(buffer);
        if (logger.isDebugEnabled()) {
            logger.debug("length read: " + buffer.length);
        }

        return buffer;
    } else {
        byte[] buffer = new byte[0];
        return buffer;
    }
}

From source file:org.mule.transport.tcp.protocols.LengthProtocol.java

public Object read(InputStream is) throws IOException {
    // original comments indicated that we need to use read(byte[]) rather than readInt()
    // to avoid socket timeouts - don't understand, but don't want to risk change.

    // first read the data necessary to know the length of the payload
    DataInputStream dis = new DataInputStream(is);
    dis.mark(SIZE_INT);/*from  ww  w  .  ja v a 2 s .  co  m*/
    // this pulls through SIZE_INT bytes
    if (null == super.read(dis, SIZE_INT)) {
        return null; // eof
    }

    // reset and read the integer
    dis.reset();
    int length = dis.readInt();
    if (logger.isDebugEnabled()) {
        logger.debug("length: " + length);
    }

    if (length < 0 || (getMaxMessageLength() > 0 && length > getMaxMessageLength())) {
        throw new IOException("Length " + length + " exceeds limit: " + getMaxMessageLength());
    }

    // finally read the rest of the data
    byte[] buffer = new byte[length];
    dis.readFully(buffer);
    if (logger.isDebugEnabled()) {
        logger.debug("length read: " + buffer.length);
    }

    return buffer;
}

From source file:org.structr.core.graph.SyncCommand.java

private static void importDatabase(final GraphDatabaseService graphDb, final SecurityContext securityContext,
        final ZipInputStream zis, boolean doValidation, final Long batchSize)
        throws FrameworkException, IOException {

    final App app = StructrApp.getInstance();
    final DataInputStream dis = new DataInputStream(new BufferedInputStream(zis));
    final RelationshipFactory relFactory = new RelationshipFactory(securityContext);
    final long internalBatchSize = batchSize != null ? batchSize : 200;
    final NodeFactory nodeFactory = new NodeFactory(securityContext);
    final String uuidPropertyName = GraphObject.id.dbName();
    final Map<String, Node> uuidMap = new LinkedHashMap<>();
    final Set<Long> deletedNodes = new HashSet<>();
    final Set<Long> deletedRels = new HashSet<>();
    final SuperUser superUser = new SuperUser();
    double t0 = System.nanoTime();
    PropertyContainer currentObject = null;
    String currentKey = null;//www  .  j a  va2 s .  c  om
    boolean finished = false;
    long totalNodeCount = 0;
    long totalRelCount = 0;

    do {

        try (final Tx tx = app.tx(doValidation)) {
            final List<Relationship> rels = new LinkedList<>();
            final List<Node> nodes = new LinkedList<>();
            long nodeCount = 0;
            long relCount = 0;

            do {

                try {

                    // store current position
                    dis.mark(4);

                    // read one byte
                    byte objectType = dis.readByte();

                    // skip newlines
                    if (objectType == '\n') {
                        continue;
                    }

                    if (objectType == 'N') {

                        // break loop after 200 objects, commit and restart afterwards
                        if (nodeCount + relCount >= internalBatchSize) {
                            dis.reset();
                            break;
                        }

                        currentObject = graphDb.createNode();
                        nodeCount++;

                        // store for later use
                        nodes.add((Node) currentObject);

                    } else if (objectType == 'R') {

                        // break look after 200 objects, commit and restart afterwards
                        if (nodeCount + relCount >= internalBatchSize) {
                            dis.reset();
                            break;
                        }

                        String startId = (String) deserialize(dis);
                        String endId = (String) deserialize(dis);
                        String relTypeName = (String) deserialize(dis);

                        Node endNode = uuidMap.get(endId);
                        Node startNode = uuidMap.get(startId);

                        if (startNode != null && endNode != null) {

                            if (deletedNodes.contains(startNode.getId())
                                    || deletedNodes.contains(endNode.getId())) {

                                System.out.println("NOT creating relationship between deleted nodes..");

                            } else {

                                RelationshipType relType = DynamicRelationshipType.withName(relTypeName);
                                currentObject = startNode.createRelationshipTo(endNode, relType);

                                // store for later use
                                rels.add((Relationship) currentObject);

                                relCount++;
                            }

                        } else {

                            System.out.println("NOT creating relationship of type " + relTypeName + ", start: "
                                    + startId + ", end: " + endId);
                        }

                    } else {

                        // reset if not at the beginning of a line
                        dis.reset();

                        if (currentKey == null) {

                            currentKey = (String) deserialize(dis);

                        } else {

                            if (currentObject != null) {

                                Object obj = deserialize(dis);

                                if (uuidPropertyName.equals(currentKey) && currentObject instanceof Node) {

                                    String uuid = (String) obj;
                                    uuidMap.put(uuid, (Node) currentObject);
                                }

                                if (currentKey.length() != 0) {

                                    // store object in DB
                                    currentObject.setProperty(currentKey, obj);

                                    // set type label
                                    if (currentObject instanceof Node
                                            && NodeInterface.type.dbName().equals(currentKey)) {
                                        ((Node) currentObject).addLabel(DynamicLabel.label((String) obj));
                                    }

                                } else {

                                    logger.log(Level.SEVERE, "Invalid property key for value {0}, ignoring",
                                            obj);
                                }

                                currentKey = null;

                            } else {

                                logger.log(Level.WARNING, "No current object to store property in.");
                            }
                        }
                    }

                } catch (EOFException eofex) {

                    finished = true;
                }

            } while (!finished);

            totalNodeCount += nodeCount;
            totalRelCount += relCount;

            for (Node node : nodes) {

                if (!deletedNodes.contains(node.getId())) {

                    NodeInterface entity = nodeFactory.instantiate(node);

                    // check for existing schema node and merge
                    if (entity instanceof AbstractSchemaNode) {
                        checkAndMerge(entity, deletedNodes, deletedRels);
                    }

                    if (!deletedNodes.contains(node.getId())) {

                        TransactionCommand.nodeCreated(superUser, entity);
                        entity.addToIndex();
                    }
                }
            }

            for (Relationship rel : rels) {

                if (!deletedRels.contains(rel.getId())) {

                    RelationshipInterface entity = relFactory.instantiate(rel);
                    TransactionCommand.relationshipCreated(superUser, entity);
                    entity.addToIndex();
                }
            }

            logger.log(Level.INFO, "Imported {0} nodes and {1} rels, committing transaction..",
                    new Object[] { totalNodeCount, totalRelCount });

            tx.success();

        }

    } while (!finished);

    double t1 = System.nanoTime();
    double time = ((t1 - t0) / 1000000000.0);

    DecimalFormat decimalFormat = new DecimalFormat("0.000000000",
            DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    logger.log(Level.INFO, "Import done in {0} s", decimalFormat.format(time));
}