|
|
1b8111 |
package com.icystar.findnumber;
|
|
|
1b8111 |
|
|
|
1b8111 |
import java.io.FileInputStream;
|
|
|
1b8111 |
import java.io.FileOutputStream;
|
|
|
1b8111 |
import java.io.IOException;
|
|
|
1b8111 |
import java.lang.reflect.Array;
|
|
|
1b8111 |
import java.nio.ByteBuffer;
|
|
|
1b8111 |
import java.util.Arrays;
|
|
|
1b8111 |
|
|
|
1b8111 |
public class Serializer {
|
|
|
1b8111 |
public static void writeInt(FileOutputStream fos, int value) throws IOException {
|
|
|
1b8111 |
fos.write( ByteBuffer.allocate((Integer.SIZE+7)/8).putInt(value).array() );
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static int readInt(FileInputStream fis) throws IOException {
|
|
|
1b8111 |
byte[] b = new byte[(Integer.SIZE+7)/8];
|
|
|
1b8111 |
Arrays.fill(b, (byte)0);
|
|
|
1b8111 |
fis.read(b);
|
|
|
1b8111 |
return ByteBuffer.wrap(b).getInt();
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static void writeFloat(FileOutputStream fos, float value) throws IOException {
|
|
|
1b8111 |
fos.write( ByteBuffer.allocate((Float.SIZE+7)/8).putFloat(value).array() );
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static float readFloat(FileInputStream fis) throws IOException {
|
|
|
1b8111 |
byte[] b = new byte[(Float.SIZE+7)/8];
|
|
|
1b8111 |
Arrays.fill(b, (byte)0);
|
|
|
1b8111 |
fis.read(b);
|
|
|
1b8111 |
return ByteBuffer.wrap(b).getFloat();
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static void writeString(FileOutputStream fos, String value) throws IOException {
|
|
|
1b8111 |
byte[] b = value.getBytes();
|
|
|
1b8111 |
writeInt(fos, b.length);
|
|
|
1b8111 |
fos.write(b);
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static String readString(FileInputStream fis) throws IOException {
|
|
|
1b8111 |
byte[] b = new byte[readInt(fis)];
|
|
|
1b8111 |
fis.read(b);
|
|
|
1b8111 |
return new String(b);
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static <t extends="" object=""> T[] resizeArray(Class<t> typeClass, T[] array, int size) {
</t></t>
|
|
|
1b8111 |
@SuppressWarnings("unchecked")
|
|
|
1b8111 |
T[] resized = (T[])Array.newInstance(typeClass, Math.max(0, size));
|
|
|
1b8111 |
if (array != null)
|
|
|
1b8111 |
for(int i = 0; i < array.length && i < resized.length; i++)
|
|
|
1b8111 |
resized[i] = array[i];
|
|
|
1b8111 |
return resized;
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static <t extends="" object=""> T[] resizeArrayIfNeed(Class<t> typeClass, T[] array, int size) {
</t></t>
|
|
|
1b8111 |
if (array != null && array.length == Math.max(0, size)) return array;
|
|
|
1b8111 |
return resizeArray(typeClass, array, Math.max(0, size));
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static <t extends="" object=""> T[] enlargeArray(Class<t> typeClass, T[] array, int size) {
</t></t>
|
|
|
1b8111 |
return resizeArray(typeClass, array, Math.max(size, array == null ? 0 : array.length));
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static <t extends="" object=""> T[] reduceArray(Class<t> typeClass, T[] array, int size) {
</t></t>
|
|
|
1b8111 |
return resizeArray(typeClass, array, Math.min(size, array == null ? 0 : array.length));
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static <t extends="" object=""> T[] enlargeArrayIfNeed(Class<t> typeClass, T[] array, int size) {
</t></t>
|
|
|
1b8111 |
return resizeArrayIfNeed(typeClass, array, Math.max(size, array == null ? 0 : array.length));
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static <t extends="" object=""> T[] reduceArrayIfNeed(Class<t> typeClass, T[] array, int size) {
</t></t>
|
|
|
1b8111 |
return resizeArrayIfNeed(typeClass, array, Math.min(size, array == null ? 0 : array.length));
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|