Example usage for java.io ObjectInputStream readFully

List of usage examples for java.io ObjectInputStream readFully

Introduction

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

Prototype

public void readFully(byte[] buf) throws IOException 

Source Link

Document

Reads bytes, blocking until all bytes are read.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    String s = "Hello World from java2s.com";

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeUTF(s);//  w w  w .  j a  v a  2  s .  c  o  m
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print the whole content
    byte[] b = new byte[13];
    ois.readFully(b);
    String array = new String(b);
    System.out.println(array);
    ois.close();
}

From source file:net.sf.jasperreports.engine.base.JRVirtualPrintPage.java

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    Object object = in.readObject();
    if (object instanceof VirtualizableElementList) {
        elements = (VirtualizableElementList) object;
    } else {/* www.j  a va  2  s  . c  o  m*/
        // page serialized by old version
        String oldUid = (String) object;
        if (log.isDebugEnabled()) {
            log.debug("Original page uid " + oldUid);
        }

        JRVirtualizationContext virtualizationContext = (JRVirtualizationContext) in.readObject();

        int length = in.readInt();
        byte[] buffer = new byte[length];
        in.readFully(buffer);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer, 0, buffer.length);
        VirtualizationObjectInputStream elementsStream = new VirtualizationObjectInputStream(inputStream,
                virtualizationContext);
        try {
            @SuppressWarnings("unchecked")
            List<JRPrintElement> elementsList = (List<JRPrintElement>) elementsStream.readObject();

            // create a new list for the elements
            elements = new VirtualizableElementList(virtualizationContext, this);
            elements.addAll(elementsList);
        } finally {
            elementsStream.close();
        }
    }
}

From source file:net.sf.jasperreports.engine.base.ElementsBlock.java

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    // we do not use the original ID as the source object might still be
    // alive in the JVM
    String oldUid = (String) in.readObject();
    uid = makeUID();/* w  w w . j  a v a2s.co  m*/
    if (log.isDebugEnabled()) {
        log.debug("Original uid " + oldUid + "; new uid " + uid);
    }

    context = (JRVirtualizationContext) in.readObject();

    int length = in.readInt();
    //FIXME put a limit on the buffer
    byte[] buffer = new byte[length];
    in.readFully(buffer);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer, 0, buffer.length);
    VirtualizationObjectInputStream elementsStream = new VirtualizationObjectInputStream(inputStream, context);
    elements = (List<JRPrintElement>) elementsStream.readObject();
    size = elements.size();

    if (!elements.isEmpty()) {
        register();
    }
}

From source file:mitm.application.djigzo.james.Certificates.java

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    try {//w w  w . j ava 2 s.  c om
        CertificateFactory certificateFactory = SecurityFactoryFactory.getSecurityFactory()
                .createCertificateFactory("X.509");

        certificates = new HashSet<X509Certificate>();

        long version = in.readLong();

        if (version != serialVersionUID) {
            throw new SerializationException("Version expected '" + serialVersionUID + "' but got '" + version);
        }

        /*
         * Read how many certificates we have to read.
         */
        int nrOfCertificates = in.readInt();

        for (int i = 0; i < nrOfCertificates; i++) {
            int encodedSize = in.readInt();

            byte[] encoded = new byte[encodedSize];

            in.readFully(encoded);

            X509Certificate certificate = (X509Certificate) certificateFactory
                    .generateCertificate(new ByteArrayInputStream(encoded));

            certificates.add(certificate);
        }
    } catch (NoSuchProviderException e) {
        throw new NoSuchProviderRuntimeException(e);
    } catch (CertificateException e) {
        throw new IOException(e);
    } catch (SecurityFactoryFactoryException e) {
        throw new IOException(e);
    }
}

From source file:com.github.jsonj.JsonObject.java

void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    // when deserializing, parse the json string
    try {/* w ww. j av a 2s .co m*/
        int length = in.readInt();
        byte[] buf = new byte[length];
        in.readFully(buf);
        if (parser == null) {
            // create it lazily, static so won't increase object size
            parser = new JsonParser();
        }
        JsonElement o = parser.parse(new String(buf, UTF8));
        Field f = getClass().getDeclaredField("intMap");
        f.setAccessible(true);
        f.set(this, new SimpleIntKeyMap<>());

        for (Entry<String, JsonElement> e : o.asObject().entrySet()) {
            put(e.getKey(), e.getValue());
        }
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.bigdata.dastor.db.ColumnFamilyStore.java

protected Set<String> readSavedCache(File path, boolean sort) throws IOException {
    Set<String> keys;
    if (sort) {/*from  ww  w.j  a  v a2  s . c  o  m*/
        // sort the results on read because cache may be written many times during server lifetime,
        // so better to pay that price once on startup than sort at write time.
        keys = new TreeSet<String>(StorageProxy.keyComparator);
    } else {
        keys = new HashSet<String>();
    }

    long start = System.currentTimeMillis();
    if (path.exists()) {
        if (logger_.isDebugEnabled())
            logger_.debug("reading saved cache from " + path);
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));
        Charset UTF8 = Charset.forName("UTF-8");
        while (in.available() > 0) {
            int size = in.readInt();
            byte[] bytes = new byte[size];
            in.readFully(bytes);
            keys.add(new String(bytes, UTF8));
        }
        in.close();
        if (logger_.isDebugEnabled())
            logger_.debug(String.format("completed reading (%d ms; %d keys) from saved cache at %s",
                    (System.currentTimeMillis() - start), keys.size(), path));
    }

    return keys;
}

From source file:org.eclipse.wb.internal.core.utils.IOUtils2.java

/**
 * @return the byte[] array, read length and then bytes.
 *///from   w  w w .  j a v a 2s .  co  m
public static byte[] readByteArray(ObjectInputStream ois) throws IOException {
    int length = ois.readInt();
    byte[] bytes = new byte[length];
    ois.readFully(bytes);
    return bytes;
}