objectstateutils.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,644 行 · 第 1/4 页

JAVA
1,644
字号

					byte[] data = test_outBuffer.toByteArray();
					out.writeBoolean(ACTIVE_OBJECT);
					out.writeObject(data);

					test_objOut.close();
					test_outBuffer.close();
					savedListSize++;
				} catch (NotSerializableException nse2) {
					// process this exception
					traceNotSerializable(obj, nse2, desc,
							"ObjectStateUtils.writeArrayList()",
							OBJ_SAVE_PROBLEM);
				} catch (Exception exc) {
					// use this as a generic point for exceptions

					// trace point
					if (log.isTraceEnabled()) {
						log.trace("ObjectStateUtils:writeArrayList(): object["
								+ obj.getClass().getName()
								+ "]  ***Exception***  ["
								+ exc.getClass().getName() + " : "
								+ exc.getMessage() + "]  " + OBJ_SAVE_PROBLEM,
								exc);
						// exc.printStackTrace();
					}
				}
			}

			// put the end-of-marker in the stream
			out.writeBoolean(EMPTY_OBJECT);
		}
		// trace point
		if (log.isTraceEnabled()) {
			log.trace("ObjectStateUtils:writeArrayList(): List [" + desc
					+ "]   members saved [" + savedListSize + "]");
		}
	}

	/**
	 * Reads an array of objects from the specified input stream. Returns null
	 * if no array is available. <p/> The format of the information to be read
	 * from the input stream should be
	 * <LI> class name
	 * <LI> active or empty
	 * <LI> data <p/> NOTE: each object in the array should implement either
	 * java.io.Serializable or java.io.Externalizable in order to be saved <p/>
	 * 
	 * @param in
	 *            The input stream
	 * @param desc
	 *            A text description to use for logging
	 * @return The ArrayList or null, if not available
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static ArrayList readArrayList(ObjectInput in, String desc)
			throws IOException {
		// The format of the data is
		//
		// Non-null list:
		// UTF - description string
		// boolean - active flag
		// objects - objects from list
		// - ACTIVE_OBJECT
		// - data
		// EMPTY_OBJEXT - end of array marker
		//    
		// Null list:
		// UTF - description string
		// boolean - empty flag
		//

		ArrayList list = null;

		String str_desc = in.readUTF();

		boolean isActive = in.readBoolean();

		if (isActive == ACTIVE_OBJECT) {
			list = new ArrayList();

			// stop when we get to the end-of-list marker
			while (in.readBoolean()) {

				// get the object
				try {
					byte[] data = (byte[]) in.readObject();

					// convert the byte[] back into the real object
					ByteArrayInputStream test_inBuffer = new ByteArrayInputStream(
							data);
					ObjectInputStream test_objIn = new ObjectInputStream(
							test_inBuffer);
					Object obj = test_objIn.readObject();
					test_objIn.close();
					test_inBuffer.close();

					// add the entry to the list
					list.add(obj);

					// trace point
					if (log.isTraceEnabled()) {
						log.trace("ObjectStateUtils:readArrayList(): [" + desc
								+ "]  index [" + list.size() + "]  for saved ["
								+ str_desc + "]");
					}
				} catch (Exception ex) {
					// use this as a generic point for all exceptions

					// trace point
					if (log.isTraceEnabled()) {
						log.trace(
								"ObjectStateUtils:readArrayList(): [" + desc
										+ "]  object index [" + list.size()
										+ "]   for saved [" + str_desc
										+ "]  ***Exception***  ["
										+ ex.getClass().getName() + " : "
										+ ex.getMessage() + "]  "
										+ OBJ_RESTORE_PROBLEM, ex);
						// ex.printStackTrace();
					}
				}

			} // end while keep going
		}

		// trace point
		if (log.isTraceEnabled()) {
			int size = (list == null) ? -1 : list.size();
			log.trace("ObjectStateUtils:readArrayList(): [" + desc
					+ "]  returning  [listsize=" + size + "]  for saved ["
					+ str_desc + "]");
		}
		return list;
	}

	/**
	 * Write a hashmap of objects to the specified output stream. <p/> The
	 * format of the information written to the output stream is
	 * <LI> class name of the array
	 * <LI> active or empty
	 * <LI> data <p/> NOTE: each object in the map should implement either
	 * java.io.Serializable or java.io.Externalizable in order to be saved <p/>
	 * 
	 * @param out
	 *            The output stream
	 * @param map
	 *            The HashMap to write
	 * @param desc
	 *            A text description to use for logging
	 * @throws IOException
	 *             Exception
	 */
	public static void writeHashMap(ObjectOutput out, HashMap map, String desc)
			throws IOException {
		// The format of the data is
		//
		// Non-null map:
		// UTF - description string
		// boolean - active flag
		// objects - object,object pairs from list
		// - active flag
		// - key
		// - value
		// EMPTY OBJECT - end marker
		//    
		// Empty list:
		// UTF - description string
		// boolean - empty flag
		//
		int savedMapSize = 0;

		out.writeUTF(desc);
		out.writeBoolean(map == null ? EMPTY_OBJECT : ACTIVE_OBJECT);

		if (map != null) {
			Set keyset = map.keySet();
			Iterator i = keyset.iterator();

			while (i.hasNext()) {
				// handle errors when can't access the value for the key

				Object key = i.next();
				Object value = map.get(key);

				try {
					// put each pair into a buffer to see if they can be saved
					ByteArrayOutputStream pair_outBuffer = new ByteArrayOutputStream();
					ObjectOutputStream pair_objOut = new ObjectOutputStream(
							pair_outBuffer);

					// write the objects in pairs
					pair_objOut.writeObject(key);
					pair_objOut.writeObject(value);
					pair_objOut.flush();

					byte[] data = pair_outBuffer.toByteArray();
					out.writeBoolean(ACTIVE_OBJECT);
					out.writeObject(data);

					pair_objOut.close();
					pair_outBuffer.close();
					savedMapSize++;
				} catch (NotSerializableException nse2) {
					// only trace the first time a particular class causes this
					// exception
					traceNotSerializable(key, nse2, desc,
							"ObjectStateUtils.writeHashMap() map key",
							OBJ_SAVE_PROBLEM);
				} catch (Exception exc) {
					// use this as a generic point for exceptions

					// trace point
					if (log.isTraceEnabled()) {
						log.trace("ObjectStateUtils:writeHashMap(): map key ["
								+ key.getClass().getName()
								+ "]  ***Exception***  ["
								+ exc.getClass().getName() + " : "
								+ exc.getMessage() + "]  " + OBJ_SAVE_PROBLEM,
								exc);
						// exc.printStackTrace();
					}
				}
			}

			// write out a marker for the end of list
			out.writeBoolean(EMPTY_OBJECT);

		}

		// trace point
		if (log.isTraceEnabled()) {
			log.trace("ObjectStateUtils:writeHashMap(): map [" + desc
					+ "]   members saved [" + savedMapSize + "]");
		}
	}

	/**
	 * Read a hashmap of objects from the specified input stream. Returns null
	 * if no hashmap is available. <p/> The format of the information to be read
	 * from the input stream should be
	 * <LI> class name
	 * <LI> active or empty
	 * <LI> data <p/> NOTE: each object in the array should implement either
	 * java.io.Serializable or java.io.Externalizable in order to be saved <p/>
	 * 
	 * @param in
	 *            The input stream
	 * @param desc
	 *            A text description to use for logging
	 * @return The HashMap or null, if not available
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static HashMap readHashMap(ObjectInput in, String desc)
			throws IOException {
		// The format of the data is
		//
		// Non-null map:
		// UTF - description string
		// boolean - active flag
		// objects - object,object pairs from list
		// - active flag
		// - key
		// - value
		// EMPTY OBJECT - end marker
		//    
		// Empty list:
		// UTF - description string
		// boolean - empty flag
		//
		int obtainedMapSize = 0;

		HashMap map = null;
		String str_desc = in.readUTF();
		boolean isActive = in.readBoolean();

		if (isActive == ACTIVE_OBJECT) {
			map = new HashMap();

			while (in.readBoolean()) {
				Object key = null;
				Object value = null;

				try {
					byte[] data = (byte[]) in.readObject();

					// convert the byte[] back into the real objects
					ByteArrayInputStream test_inBuffer = new ByteArrayInputStream(
							data);
					ObjectInputStream test_objIn = new ObjectInputStream(
							test_inBuffer);
					key = test_objIn.readObject();
					value = test_objIn.readObject();
					test_objIn.close();
					test_inBuffer.close();

					// add the entry to the map
					map.put(key, value);
					obtainedMapSize++;

					// trace point
					if (log.isTraceEnabled()) {
						log.trace("ObjectStateUtils:readHashMap(): [" + desc
								+ "]  object pair index [" + obtainedMapSize
								+ "]   for saved [" + str_desc + "]");
					}
				} catch (Exception ex) {
					// use this as a generic point for all exceptions

					// trace point
					if (log.isTraceEnabled()) {
						log.trace(
								"ObjectStateUtils:readHashMap(): [" + desc
										+ "]  object pair index ["
										+ obtainedMapSize + "]   for saved ["
										+ str_desc + "] ***Exception***  ["
										+ ex.getClass().getName() + " : "
										+ ex.getMessage() + "]  "
										+ OBJ_RESTORE_PROBLEM, ex);
						// ex.printStackTrace();
					}
				}
			}
		}

		int size = (map == null) ? -1 : map.size();
		if (log.isTraceEnabled()) {
			log.trace("ObjectStateUtils:readHashMap(): [" + desc
					+ "]  returning  [mapsize=" + size + "]    for saved ["
					+ str_desc + "]");
		}

		return map;
	}

	/**
	 * Write a linked list of objects to the specified output stream. <p/> The
	 * format of the information written to the output stream is
	 * <LI> class name of the array
	 * <LI> active or empty
	 * <LI> data <p/> NOTE: each object in the array should implement either
	 * java.io.Serializable or java.io.Externalizable in order to be saved <p/>
	 * 
	 * @param out
	 *            The output stream
	 * @param list
	 *            The LinkedList to write
	 * @param desc
	 *            A text description to use for logging
	 * @throws IOException
	 *             Exception
	 */
	public static void writeLinkedList(ObjectOutput out, LinkedList objlist,
			String desc) throws IOException {
		// The format of the data is
		//
		// Non-null list:
		// UTF - description string
		// boolean - active flag
		// objects - objects from list
		// - ACTIVE_OBJECT
		// - data
		// EMPTY_OBJEXT - end of array marker
		//    
		// Null list:
		// UTF - description string
		// boolean - empty flag
		//
		int savedListSize = 0;

		out.writeUTF(desc);
		out.writeBoolean(objlist == null ? EMPTY_OBJECT : ACTIVE_OBJECT);

		if (objlist != null) {
			// setup an iterator for the list
			Iterator i = objlist.iterator();

			while (i.hasNext()) {
				Object obj = i.next();

				try {
					// put each list entry into a test output buffer to see if
					// it can be saved
					// this technique preserves the integrity of the real output
					// stream in the
					// event of a serialization error
					ByteArrayOutputStream test_outBuffer = new ByteArrayOutputStream();
					ObjectOutputStream test_objOut = new ObjectOutputStream(
							test_outBuffer);

					// write the object to the test buffer
					test_objOut.writeObject(obj);
					test_objOut.flush();

					byte[] data = test_outBuffer.toByteArray();
					out.writeBoolean(ACTIVE_OBJECT);
					out.writeObject(data);

					test_objOut.close();
					test_outBuffer.close();
					savedListSize++;
				} catch (NotSerializableException nse2) {
					// process this exception
					traceNotSerializable(obj, nse2, desc,
							"ObjectStateUtils.writeLinkedList()",
							OBJ_SAVE_PROBLEM);
				} catch (Exception exc) {
					// use this as a generic point for exceptions

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?