Serialize an object to a byte array.

@param pk the object
@throws IOException if an error occurs during the serialization process.
484309
public static byte[] serObjByte(Object pk) throws IOException {
        final boolean trace = TraceComponent.isAnyTracingEnabled();
        if (trace && tc.isEntryEnabled())
            Tr.entry(tc, "serObjByte", pk == null ? null : pk.getClass());
        byte[] b;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bos);
            out.writeObject(pk);
            out.flush();
            out.close();
            b = bos.toByteArray();
        } catch (IOException e) {
            FFDCFilter.processException(e, Utils.class.getName(), "336");
            if (trace && tc.isEntryEnabled())
                Tr.exit(tc, "serObjByte", new Object[] { "Unable to serialize: " + pk, e });
            throw e;
        } catch (Error e) {
            FFDCFilter.processException(e, Utils.class.getName(), "342");
            if (trace && tc.isEntryEnabled())
                Tr.exit(tc, "serObjByte", new Object[] { "Unable to serialize: " + pk, e });
            throw e;
        }
        if (trace && tc.isEntryEnabled())
            Tr.exit(tc, "serObjByte");
        return b;
    }
539512
@Override
    public byte[] serialize() {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(map);
            return out.toByteArray();
        } catch (IOException ex) {
            throw new IllegalStateException("Unable to compress SerializableMap");
        }
    }
125346
public static <T extends Serializable> byte[] serializeIntoBytes(T obj) throws IOException {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
      oos.writeObject(obj);
      oos.flush();
      return bos.toByteArray();
    }
  }
1182540
public static byte[] serialize(Object obj) throws IOException {
    if (obj == null) {
      return null;
    }
    try (ByteArrayOutputStream b = new ByteArrayOutputStream()) {
      try (ObjectOutputStream o = new ObjectOutputStream(b)) {
        o.writeObject(obj);
      }
      return b.toByteArray();
    }
  }
403555
@Override
    public void serialize(ConstraintViolationImpl constraintViolation, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("message", constraintViolation.getMessage());
        Object invalidValue = constraintViolation.getInvalidValue();
        if (invalidValue != null) {
            jsonGenerator.writeStringField("invalid", invalidValue.toString());
        }
        if (constraintViolation.getPropertyPath() != null) {
            jsonGenerator.writeStringField("path", constraintViolation.getPropertyPath().toString());
        }
        jsonGenerator.writeEndObject();
    }
460677
public final ByteBuffer serialize () throws InternetSCSIException , IOException {

        basicHeaderSegment.getParser().checkIntegrity();

        final ByteBuffer pdu = ByteBuffer.allocate(calcSize());

        int offset = 0;
        offset += basicHeaderSegment.serialize(pdu, offset);

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Serialized Basic Header Segment:\n" + toString());
        }

        offset += serializeAdditionalHeaderSegments(pdu, offset);
        
        // write header digest
        // TODO: Move CRC calculation in BasicHeaderSegment.serialize?
        if (basicHeaderSegment.getParser().canHaveDigests()) {
            offset += serializeDigest(pdu, headerDigest);
        }

        // serialize data segment
        offset += serializeDataSegment(pdu, offset);

        // write data segment digest
        // TODO: Move CRC calculation in BasicHeaderSegment.serialize?
        if (basicHeaderSegment.getParser().canHaveDigests()) {
            offset += serializeDigest(pdu, dataDigest);
        }

        return (ByteBuffer) pdu.rewind();
    }
541297
public static void serialize(File file, SerializableObject object, @NonNull EncryptionKey encryptionKey) throws IOException {
		ByteArrayOutputStream bos = null;
		DataOutputStream dos = null;
		try {
			bos = new ByteArrayOutputStream();
			dos = new DataOutputStream(bos);
			object.writeExternal(dos);
			final byte[] unencryptedBytes = bos.toByteArray();
			final byte[] encryptedBytes = Encryptor.encrypt(encryptionKey, unencryptedBytes);
			Util.writeAtomically(file, encryptedBytes);
		} catch (Exception e) {
			throw new IOException(e);
		} finally {
			Util.ensureClosed(bos);
			Util.ensureClosed(dos);
		}
	}
79332
public static <S extends Serializable> S deserialize(byte[] bytes) throws IOException {
    if (bytes == null) {
      return null;
    }
    return deserialize(new ByteArrayInputStream(bytes));
  }
755358
public static byte[] serialize(Object value) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    serialize(value, out);
    return out.toByteArray();
  }
1373093
public byte[] serializeToBytes(final Serializable o) {
        byte[] bytes = null;
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
             final ObjectOutputStream oos = new ObjectOutputStream(baos)) {
            oos.writeObject(o);
            oos.flush();
            bytes = baos.toByteArray();
        } catch (final IOException e) {
            logger.warn("cannot Java serialize object", e);
        }
        return bytes;
    }
986629
public void serialize(final Object obj, final OutputStream stream, final int bufferSize) {
    ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize);
    Kryo kryo = borrow();
    try {
      kryo.writeClassAndObject(out, obj);
      out.flush();
    } finally {
      release(kryo);
    }
  }
329968
public static byte[] serialize(FastSerializable object) throws IOException {
        FastSerializer out = new FastSerializer();
        object.writeExternal(out);
        return out.getBBContainer().b().array();
    }
245330
public static <E> String serialize(Map<String, E> map, Class<E> jsonObjectClass) throws IOException {
        return mapperFor(jsonObjectClass).serialize(map);
    }
287481
@Override
  public void serialize(DataOutput out) throws IOException {
    if (buffer.hasArray()) {
      out.write(buffer.array(), buffer.arrayOffset(), buffer.limit());
    } else {
      ByteBuffer tmp = buffer.duplicate();
      tmp.position(0);
      try (WritableByteChannel channel = Channels.newChannel((OutputStream) out)) {
        channel.write(tmp);
      }
    }
  }
1083989
@Override
    public final int read(byte[] pBytes) throws IOException {
        return read(pBytes, 0, pBytes != null ? pBytes.length : 1);
    }
147000
public void write(byte[] b)
        throws IOException
    {
        ensureSpareCapacity(b.length);
        System.arraycopy(b,0,_buf,_pos,b.length);
        _pos+=b.length;
    }
377085
public static void serialize(File file, SerializableObject object) throws IOException {
		AtomicFile atomicFile = new AtomicFile(file);
		FileOutputStream stream = null;
		try {
			stream = atomicFile.startWrite();
			DataOutputStream out = new DataOutputStream(stream);
			object.writeExternal(out);
			atomicFile.finishWrite(stream); // serialization was successful
		} catch (Exception e) {
			atomicFile.failWrite(stream); // serialization failed
			throw new IOException(e); // throw exception up the chain
		}
	}