1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
53: public final class Buffers
54: {
55:
62: public static DataBuffer createBuffer(int dataType, Object data,
63: int size)
64: {
65: if (data == null) return createBuffer(dataType, size, 1);
66:
67: return createBufferFromData(dataType, data, size);
68: }
69:
70:
71:
77: public static DataBuffer createBuffer(int dataType, int size) {
78: return createBuffer(dataType, size, 1);
79: }
80:
81:
88: public static DataBuffer createBuffer(int dataType, int size, int numBanks)
89: {
90: switch (dataType)
91: {
92: case DataBuffer.TYPE_BYTE:
93: return new DataBufferByte(size, numBanks);
94: case DataBuffer.TYPE_SHORT:
95: return new DataBufferShort(size, numBanks);
96: case DataBuffer.TYPE_USHORT:
97: return new DataBufferUShort(size, numBanks);
98: case DataBuffer.TYPE_INT:
99: return new DataBufferInt(size, numBanks);
100: case DataBuffer.TYPE_FLOAT:
101: return new DataBufferFloat(size, numBanks);
102: case DataBuffer.TYPE_DOUBLE:
103: return new DataBufferDouble(size, numBanks);
104: default:
105: throw new UnsupportedOperationException();
106: }
107: }
108:
109:
116: public static DataBuffer createBufferFromData(int dataType, Object data,
117: int size)
118: {
119: switch (dataType)
120: {
121: case DataBuffer.TYPE_BYTE:
122: return new DataBufferByte((byte[]) data, size);
123: case DataBuffer.TYPE_SHORT:
124: return new DataBufferShort((short[]) data, size);
125: case DataBuffer.TYPE_USHORT:
126: return new DataBufferUShort((short[]) data, size);
127: case DataBuffer.TYPE_INT:
128: return new DataBufferInt((int[]) data, size);
129: case DataBuffer.TYPE_FLOAT:
130: return new DataBufferFloat((float[]) data, size);
131: case DataBuffer.TYPE_DOUBLE:
132: return new DataBufferDouble((double[]) data, size);
133: default:
134: throw new UnsupportedOperationException();
135: }
136: }
137:
138:
145: public static Object getData(DataBuffer buffer)
146: {
147: return getData(buffer, 0, null, 0, buffer.getSize());
148: }
149:
150:
151:
156: public static Object getData(DataBuffer src, int srcOffset,
157: Object dest, int dstOffset,
158: int length)
159: {
160: Object from;
161: switch(src.getDataType())
162: {
163: case DataBuffer.TYPE_BYTE:
164: if (dest == null) dest = new byte[length+dstOffset];
165: for(int i = 0; i < length; i++)
166: ((byte[])dest)[i + dstOffset] = (byte)src.getElem(i + srcOffset);
167: break;
168:
169: case DataBuffer.TYPE_DOUBLE:
170: if (dest == null) dest = new double[length+dstOffset];
171: for(int i = 0; i < length; i++)
172: ((double[])dest)[i + dstOffset] = src.getElemDouble(i + srcOffset);
173: break;
174:
175: case DataBuffer.TYPE_FLOAT:
176: if (dest == null) dest = new float[length+dstOffset];
177: for(int i = 0; i < length; i++)
178: ((float[])dest)[i + dstOffset] = src.getElemFloat(i + srcOffset);
179: break;
180:
181: case DataBuffer.TYPE_INT:
182: if (dest == null) dest = new int[length+dstOffset];
183: for(int i = 0; i < length; i++)
184: ((int[])dest)[i + dstOffset] = src.getElem(i + srcOffset);
185: break;
186:
187: case DataBuffer.TYPE_SHORT:
188: case DataBuffer.TYPE_USHORT:
189: if (dest == null) dest = new short[length+dstOffset];
190: for(int i = 0; i < length; i++)
191: ((short[])dest)[i + dstOffset] = (short)src.getElem(i + srcOffset);
192: break;
193:
194: case DataBuffer.TYPE_UNDEFINED:
195: throw new ClassCastException("Unknown data buffer type");
196: }
197: return dest;
198: }
199:
200:
206: public static int smallestAppropriateTransferType(int bits)
207: {
208: if (bits <= 8)
209: {
210: return DataBuffer.TYPE_BYTE;
211: }
212: else if (bits <= 16)
213: {
214: return DataBuffer.TYPE_USHORT;
215: }
216: else if (bits <= 32)
217: {
218: return DataBuffer.TYPE_INT;
219: }
220: else
221: {
222: return DataBuffer.TYPE_UNDEFINED;
223: }
224: }
225: }