001: package com.quadcap.sql.io;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042: import java.io.ObjectInput;
043: import java.io.ObjectOutput;
044:
045: /**
046: * Serialization/deserialization utilities for arrays of bytes
047: * or ints.
048: *
049: * @author Stan Bailes
050: */
051: public class Arrays {
052: public static void writeBytes(ObjectOutput out, byte[] bytes)
053: throws IOException {
054: if (bytes == null) {
055: out.write(0);
056: } else {
057: out.write(1);
058: out.writeInt(bytes.length);
059: out.write(bytes);
060: }
061: }
062:
063: public static byte[] readBytes(ObjectInput in) throws IOException {
064: byte[] ret = null;
065: if (in.read() == 1) {
066: ret = new byte[in.readInt()];
067: in.read(ret);
068: }
069: return ret;
070: }
071:
072: public static void writeInts(ObjectOutput out, int[] ints)
073: throws IOException {
074: if (ints == null) {
075: out.write(0);
076: } else {
077: out.write(1);
078: out.writeInt(ints.length);
079: for (int i = 0; i < ints.length; i++) {
080: out.writeInt(ints[i]);
081: }
082: }
083: }
084:
085: public static int[] readInts(ObjectInput in) throws IOException {
086: int[] ret = null;
087: if (in.read() == 1) {
088: ret = new int[in.readInt()];
089: for (int i = 0; i < ret.length; i++) {
090: ret[i] = in.readInt();
091: }
092: }
093: return ret;
094: }
095:
096: public static void writeLongs(ObjectOutput out, long[] arr)
097: throws IOException {
098: if (arr == null) {
099: out.write(0);
100: } else {
101: out.write(1);
102: out.writeInt(arr.length);
103: for (int i = 0; i < arr.length; i++) {
104: out.writeLong(arr[i]);
105: }
106: }
107: }
108:
109: public static long[] readLongs(ObjectInput in) throws IOException {
110: long[] ret = null;
111: if (in.read() == 1) {
112: ret = new long[in.readInt()];
113: for (int i = 0; i < ret.length; i++) {
114: ret[i] = in.readLong();
115: }
116: }
117: return ret;
118: }
119:
120: }
|