/*
Derby - Class org.apache.derby.iapi.services.io.ArrayUtil
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derby.iapi.services.io;
import java.io.ObjectOutput;
import java.io.ObjectInput;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
Utility class for constructing and reading and writing arrays from/to
formatId streams and for performing other operations on arrays.
@version 0.1
*/
public abstract class ArrayUtil
{
///////////////////////////////////////////////////////////////////
//
// Methods to copy arrays.
//
///////////////////////////////////////////////////////////////////
/** Copy an array of objects; the original array could be null */
public static T[] copy( T[] original )
{
return (original == null) ?
null :
Arrays.copyOf(original, original.length);
}
/** Copy a (possibly null) array of booleans */
public static boolean[] copy( boolean[] original )
{
return (original == null) ? null : (boolean[]) original.clone();
}
/** Copy a (possibly null) array of bytes */
public static byte[] copy( byte[] original )
{
return (original == null) ? null : (byte[]) original.clone();
}
/** Copy a (possibly null) array of ints */
public static int[] copy( int[] original )
{
return (original == null) ? null : (int[]) original.clone();
}
/** Copy a (possibly null) array of longs */
public static long[] copy( long[] original )
{
return (original == null) ? null : (long[]) original.clone();
}
/** Copy a (possibly null) 2-dimensional array of ints */
public static int[][] copy2( int[][] original )
{
if ( original == null ) { return null; }
int[][] result = new int[ original.length ][];
for ( int i = 0; i < original.length; i++ )
{
result[ i ] = copy( original[ i ] );
}
return result;
}
/**
* Make the contents of an array available as a read-only list. If the
* array is null, an empty list will be returned.
*/
public static List asReadOnlyList(T... array) {
if (array == null) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(Arrays.asList(array));
}
}
///////////////////////////////////////////////////////////////////
//
// Methods for Arrays of OBJECTS. Cannot be used for an
// array of primitives, see below for something for primitives
//
///////////////////////////////////////////////////////////////////
/**
Write the length of an array of objects to an output stream.
The length
@param out ObjectOutput stream
@param a array of objects whose length should be written.
@exception java.io.IOException The write caused an IOException.
*/
public static void writeArrayLength(ObjectOutput out, Object[] a)
throws IOException
{
out.writeInt(a.length);
}
/**
Write an array of objects to an output stream.
@param out Object output stream to write to.
@param a array of objects to write.
@exception java.io.IOException The write caused an IOException.
*/
public static void writeArrayItems(ObjectOutput out, Object[] a)
throws IOException
{
if (a == null)
return;
for(int ix = 0; ix < a.length; ix++)
{ out.writeObject(a[ix]); }
}
/**
Write an array of objects and length to an output stream.
Does equivalent of writeArrayLength() followed by writeArrayItems()
@param out Object output stream to write to.
@param a array of objects to write.
@exception java.io.IOException The write caused an IOException.
*/
public static void writeArray(ObjectOutput out, Object[] a)
throws IOException
{
if (a == null)
{
out.writeInt(0);
return;
}
out.writeInt(a.length);
for(int ix = 0; ix < a.length; ix++)
{ out.writeObject(a[ix]); }
}
/**
Read an array of objects out of a stream.
@param in Input stream
@param a array to read into
@exception java.io.IOException The write caused an IOException.
@exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist
*/
public static void readArrayItems(ObjectInput in, Object[] a)
throws IOException, ClassNotFoundException
{
for (int ix=0; ix 0) {
stringArray = new String[size];
readArrayItems(in, stringArray);
}
return stringArray;
}
///////////////////////////////////////////////////////////////////
//
// Methods for Arrays of BOOLEANS
//
///////////////////////////////////////////////////////////////////
/**
Write an array of booleans to an ObjectOutput. This writes the array
in a format readBooleanArray understands.
@param out the ObjectOutput.
@param a the array.
@exception java.io.IOException The write caused an IOException.
*/
public static void writeBooleanArray(ObjectOutput out, boolean[] a) throws IOException {
if (a == null)
out.writeInt(0);
else {
out.writeInt(a.length);
for (int i=0; i
Ads help maintain this website.