Search in sources :

Example 21 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hbase by apache.

the class HbaseObjectWritableFor96Migration method writeObject.

/**
   * Write a {@link Writable}, {@link String}, primitive type, or an array of
   * the preceding.
   * @param out
   * @param instance
   * @param declaredClass
   * @param conf
   * @throws IOException
   */
@SuppressWarnings("unchecked")
static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf) throws IOException {
    Object instanceObj = instance;
    Class declClass = declaredClass;
    if (instanceObj == null) {
        // null
        instanceObj = new NullInstance(declClass, conf);
        declClass = Writable.class;
    }
    writeClassCode(out, declClass);
    if (declClass.isArray()) {
        // byte-at-a-time we were previously doing.
        if (declClass.equals(byte[].class)) {
            Bytes.writeByteArray(out, (byte[]) instanceObj);
        } else {
            //if it is a Generic array, write the element's type
            if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) {
                Class<?> componentType = declaredClass.getComponentType();
                writeClass(out, componentType);
            }
            int length = Array.getLength(instanceObj);
            out.writeInt(length);
            for (int i = 0; i < length; i++) {
                Object item = Array.get(instanceObj, i);
                writeObject(out, item, item.getClass(), conf);
            }
        }
    } else if (List.class.isAssignableFrom(declClass)) {
        List list = (List) instanceObj;
        int length = list.size();
        out.writeInt(length);
        for (int i = 0; i < length; i++) {
            Object elem = list.get(i);
            writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf);
        }
    } else if (declClass == String.class) {
        // String
        Text.writeString(out, (String) instanceObj);
    } else if (declClass.isPrimitive()) {
        // primitive type
        if (declClass == Boolean.TYPE) {
            // boolean
            out.writeBoolean(((Boolean) instanceObj).booleanValue());
        } else if (declClass == Character.TYPE) {
            // char
            out.writeChar(((Character) instanceObj).charValue());
        } else if (declClass == Byte.TYPE) {
            // byte
            out.writeByte(((Byte) instanceObj).byteValue());
        } else if (declClass == Short.TYPE) {
            // short
            out.writeShort(((Short) instanceObj).shortValue());
        } else if (declClass == Integer.TYPE) {
            // int
            out.writeInt(((Integer) instanceObj).intValue());
        } else if (declClass == Long.TYPE) {
            // long
            out.writeLong(((Long) instanceObj).longValue());
        } else if (declClass == Float.TYPE) {
            // float
            out.writeFloat(((Float) instanceObj).floatValue());
        } else if (declClass == Double.TYPE) {
            // double
            out.writeDouble(((Double) instanceObj).doubleValue());
        } else if (declClass == Void.TYPE) {
        // void
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declClass);
        }
    } else if (declClass.isEnum()) {
        // enum
        Text.writeString(out, ((Enum) instanceObj).name());
    } else if (Message.class.isAssignableFrom(declaredClass)) {
        Text.writeString(out, instanceObj.getClass().getName());
        ((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out));
    } else if (Writable.class.isAssignableFrom(declClass)) {
        // Writable
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ((Writable) instanceObj).write(out);
    } else if (Serializable.class.isAssignableFrom(declClass)) {
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(instanceObj);
            byte[] value = bos.toByteArray();
            out.writeInt(value.length);
            out.write(value);
        } finally {
            if (bos != null)
                bos.close();
            if (oos != null)
                oos.close();
        }
    } else if (Scan.class.isAssignableFrom(declClass)) {
        Scan scan = (Scan) instanceObj;
        byte[] scanBytes = ProtobufUtil.toScan(scan).toByteArray();
        out.writeInt(scanBytes.length);
        out.write(scanBytes);
    } else {
        throw new IOException("Can't write: " + instanceObj + " as " + declClass);
    }
}
Also used : Serializable(java.io.Serializable) Message(com.google.protobuf.Message) Writable(org.apache.hadoop.io.Writable) MapWritable(org.apache.hadoop.io.MapWritable) ObjectWritable(org.apache.hadoop.io.ObjectWritable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) Scan(org.apache.hadoop.hbase.client.Scan)

Example 22 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hive by apache.

the class TestReaderWriter method test.

@Test
public void test() throws MetaException, CommandNeedRetryException, IOException, ClassNotFoundException {
    driver.run("drop table mytbl");
    driver.run("create table mytbl (a string, b int)");
    Iterator<Entry<String, String>> itr = hiveConf.iterator();
    Map<String, String> map = new HashMap<String, String>();
    while (itr.hasNext()) {
        Entry<String, String> kv = itr.next();
        map.put(kv.getKey(), kv.getValue());
    }
    WriterContext cntxt = runsInMaster(map);
    File writeCntxtFile = File.createTempFile("hcat-write", "temp");
    writeCntxtFile.deleteOnExit();
    // Serialize context.
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(writeCntxtFile));
    oos.writeObject(cntxt);
    oos.flush();
    oos.close();
    // Now, deserialize it.
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(writeCntxtFile));
    cntxt = (WriterContext) ois.readObject();
    ois.close();
    runsInSlave(cntxt);
    commit(map, true, cntxt);
    ReaderContext readCntxt = runsInMaster(map, false);
    File readCntxtFile = File.createTempFile("hcat-read", "temp");
    readCntxtFile.deleteOnExit();
    oos = new ObjectOutputStream(new FileOutputStream(readCntxtFile));
    oos.writeObject(readCntxt);
    oos.flush();
    oos.close();
    ois = new ObjectInputStream(new FileInputStream(readCntxtFile));
    readCntxt = (ReaderContext) ois.readObject();
    ois.close();
    for (int i = 0; i < readCntxt.numSplits(); i++) {
        runsInSlave(readCntxt, i);
    }
}
Also used : HashMap(java.util.HashMap) ObjectOutputStream(java.io.ObjectOutputStream) FileInputStream(java.io.FileInputStream) WriterContext(org.apache.hive.hcatalog.data.transfer.WriterContext) Entry(java.util.Map.Entry) FileOutputStream(java.io.FileOutputStream) ReaderContext(org.apache.hive.hcatalog.data.transfer.ReaderContext) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) HCatBaseTest(org.apache.hive.hcatalog.mapreduce.HCatBaseTest)

Example 23 with ObjectOutputStream

use of java.io.ObjectOutputStream in project tomcat by apache.

the class DeltaManager method serializeSessions.

/**
     * Save any currently active sessions in the appropriate persistence
     * mechanism, if any. If persistence is not supported, this method returns
     * without doing anything.
     *
     * @param currentSessions Sessions to serialize
     * @return serialized data
     * @exception IOException
     *                if an input/output error occurs
     */
protected byte[] serializeSessions(Session[] currentSessions) throws IOException {
    // Open an output stream to the specified pathname, if any
    ByteArrayOutputStream fos = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos))) {
        oos.writeObject(Integer.valueOf(currentSessions.length));
        for (int i = 0; i < currentSessions.length; i++) {
            ((DeltaSession) currentSessions[i]).writeObjectData(oos);
        }
        // Flush and close the output stream
        oos.flush();
    } catch (IOException e) {
        log.error(sm.getString("deltaManager.unloading.ioe", e), e);
        throw e;
    }
    // send object data as byte[]
    return fos.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 24 with ObjectOutputStream

use of java.io.ObjectOutputStream in project tomcat by apache.

the class DeltaRequest method serialize.

/**
     * serialize DeltaRequest
     * @see DeltaRequest#writeExternal(java.io.ObjectOutput)
     *
     * @return serialized delta request
     * @throws IOException IO error serializing
     */
protected byte[] serialize() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    writeExternal(oos);
    oos.flush();
    oos.close();
    return bos.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 25 with ObjectOutputStream

use of java.io.ObjectOutputStream in project tomcat by apache.

the class XByteBuffer method serialize.

/**
     * Serializes a message into cluster data
     * @param msg ClusterMessage
     * @return serialized content as byte[] array
     * @throws IOException Serialization error
     */
public static byte[] serialize(Serializable msg) throws IOException {
    ByteArrayOutputStream outs = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(outs);
    out.writeObject(msg);
    out.flush();
    byte[] data = outs.toByteArray();
    return data;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)958 ByteArrayOutputStream (java.io.ByteArrayOutputStream)657 ObjectInputStream (java.io.ObjectInputStream)386 ByteArrayInputStream (java.io.ByteArrayInputStream)332 IOException (java.io.IOException)312 FileOutputStream (java.io.FileOutputStream)132 Test (org.junit.Test)130 File (java.io.File)75 BufferedOutputStream (java.io.BufferedOutputStream)46 ObjectOutput (java.io.ObjectOutput)35 OutputStream (java.io.OutputStream)35 HashMap (java.util.HashMap)35 FileInputStream (java.io.FileInputStream)24 ArrayList (java.util.ArrayList)24 InputStream (java.io.InputStream)22 FileNotFoundException (java.io.FileNotFoundException)20 Serializable (java.io.Serializable)15 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14 Map (java.util.Map)13