Example usage for java.io ObjectInput read

List of usage examples for java.io ObjectInput read

Introduction

In this page you can find the example usage for java.io ObjectInput read.

Prototype

public int read(byte b[], int off, int len) throws IOException;

Source Link

Document

Reads into an array of bytes.

Usage

From source file:org.apache.shindig.gadgets.http.HttpResponse.java

/**
 * Expected layout:/*w w w . ja  va  2  s .  c o  m*/
 *
 * int - status code
 * Map<String, List<String>> - headers
 * int - length of body
 * byte array - body, of previously specified length
 */
@SuppressWarnings("unchecked")
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    httpStatusCode = in.readInt();

    // We store the multimap as a Map<String,List<String>> to insulate us from google-collections API churn
    // And to remain backwards compatible

    Map<String, List<String>> headerCopyMap = (Map<String, List<String>>) in.readObject();
    Multimap headerCopy = newHeaderMultimap();

    for (Map.Entry<String, List<String>> entry : headerCopyMap.entrySet()) {
        headerCopy.putAll(entry.getKey(), entry.getValue());
    }

    int bodyLength = in.readInt();
    responseBytes = new byte[bodyLength];
    int cnt, offset = 0;
    while ((cnt = in.read(responseBytes, offset, bodyLength)) > 0) {
        offset += cnt;
        bodyLength -= cnt;
    }
    if (offset != responseBytes.length) {
        throw new IOException(
                "Invalid body! Expected length = " + responseBytes.length + ", bytes readed = " + offset + '.');
    }

    date = getAndUpdateDate(headerCopy);
    encoding = getAndUpdateEncoding(headerCopy, responseBytes);
    headers = Multimaps.unmodifiableMultimap(headerCopy);
    metadata = Collections.emptyMap();
}

From source file:org.kepler.objectmanager.cache.ActorCacheObject.java

/**
 * deserialize this class//from   ww w  .  j  av a  2s  . c om
 * 
 *@param in
 *            Description of the Parameter
 *@exception IOException
 *                Description of the Exception
 *@exception ClassNotFoundException
 *                Description of the Exception
 */
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    if (isDebugging)
        log.debug("readExternal(" + in.getClass().getName() + ")");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];
    int numread = in.read(b, 0, 1024);
    while (numread != -1) {
        bos.write(b, 0, numread);
        numread = in.read(b, 0, 1024);
    }
    bos.flush();
    _actorString = bos.toString();
    try {
        StringReader strR = new StringReader(_actorString);
        InputSource xmlIn = new InputSource(strR);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                if (MOML_PUBLIC_ID_1.equals(publicId)) {
                    return new InputSource(MoMLParser.class.getResourceAsStream("MoML_1.dtd"));
                } else {
                    return null;
                }
            }
        });
        Document doc = builder.parse(xmlIn);
        Node rootNode = doc.getDocumentElement();
        _rootname = rootNode.getNodeName();
        NamedNodeMap nnm = rootNode.getAttributes();
        Node namenode = nnm.getNamedItem("name");
        String nameStr = namenode.getNodeValue();
        this._name = nameStr;
        NodeList probNodes = rootNode.getChildNodes();
        for (int i = 0; i < probNodes.getLength(); i++) {
            Node child = probNodes.item(i);
            if (child.hasAttributes()) {
                NamedNodeMap childAttrs = child.getAttributes();
                Node idNode = childAttrs.getNamedItem("name");
                if (idNode != null) {
                    String nameval = idNode.getNodeValue();
                    if (nameval.equals(NamedObjId.NAME)) {
                        Node idNode1 = childAttrs.getNamedItem("value");
                        String idval = idNode1.getNodeValue();
                        this._lsid = new KeplerLSID(idval);
                    }
                    if (nameval.equals("class")) {
                        Node idNode3 = childAttrs.getNamedItem("value");
                        String classname = idNode3.getNodeValue();
                        this._className = classname;
                    }
                    if (nameval.startsWith("semanticType")) {
                        Node idNode2 = childAttrs.getNamedItem("value");
                        String semtype = idNode2.getNodeValue();
                        _semanticTypes.add(semtype);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Error in ActorCacheObject(ReadExternal): " + e.getMessage());
    }
}