001package net.gdface.utils;
002
003import java.nio.ByteBuffer;
004import java.nio.DoubleBuffer;
005import java.nio.FloatBuffer;
006import java.nio.IntBuffer;
007import java.nio.LongBuffer;
008import java.nio.ShortBuffer;
009
010/**
011 * NIO Buffer工具类
012 * @author guyadong
013 *
014 */
015public class BufferUtils {
016
017        /**
018         * {@link DoubleBuffer} TO {@link ByteBuffer}
019         * @param input
020         * @return
021         */
022        public static ByteBuffer asByteBuffer(DoubleBuffer input){
023                if(null == input ){
024                        return null;
025                }
026                ByteBuffer buffer = ByteBuffer.allocate(input.capacity()* (Double.SIZE/8));
027                while(input.hasRemaining()){
028                        buffer.putDouble(input.get());
029                }
030                return buffer;
031        }
032        /**
033         * double[] TO byte[]
034         * @param input
035         * @return
036         */
037        public static byte[] asByteArray(double[] input){
038                if(null == input ){
039                        return null;
040                }
041                return asByteBuffer(DoubleBuffer.wrap(input)).array();
042        }
043        /**
044         * {@link FloatBuffer} TO {@link ByteBuffer}
045         * @param input
046         * @return
047         */
048        public static ByteBuffer asByteBuffer(FloatBuffer input){
049                if(null == input ){
050                        return null;
051                }
052                ByteBuffer buffer = ByteBuffer.allocate(input.capacity()* (Float.SIZE/8));
053                while(input.hasRemaining()){
054                        buffer.putFloat(input.get());
055                }
056                return buffer;
057        }
058        /**
059         * float[] TO byte[]
060         * @param input
061         * @return
062         */
063        public static byte[] asByteArray(float[] input){
064                if(null == input ){
065                        return null;
066                }
067                return asByteBuffer(FloatBuffer.wrap(input)).array();
068        }
069        /**
070         * {@link IntBuffer} TO {@link ByteBuffer}
071         * @param input
072         * @return
073         */
074        public static ByteBuffer asByteBuffer(IntBuffer input){
075                if(null == input ){
076                        return null;
077                }
078                ByteBuffer buffer = ByteBuffer.allocate(input.capacity()* (Integer.SIZE/8));
079                while(input.hasRemaining()){
080                        buffer.putInt(input.get());
081                }
082                return buffer;
083        }
084        /**
085         * int[] TO byte[]
086         * @param input
087         * @return
088         */
089        public static byte[] asByteArray(int[] input){
090                if(null == input ){
091                        return null;
092                }
093                return asByteBuffer(IntBuffer.wrap(input)).array();
094        }
095        /**
096         * {@link LongBuffer} TO {@link ByteBuffer}
097         * @param input
098         * @return
099         */
100        public static ByteBuffer asByteBuffer(LongBuffer input){
101                if(null == input ){
102                        return null;
103                }
104                ByteBuffer buffer = ByteBuffer.allocate(input.capacity()* (Long.SIZE/8));
105                while(input.hasRemaining()){
106                        buffer.putLong(input.get());
107                }
108                return buffer;
109        }
110        /**
111         * long[] TO byte[]
112         * @param input
113         * @return
114         */
115        public static byte[] asByteArray(long[] input){
116                if(null == input ){
117                        return null;
118                }
119                return asByteBuffer(LongBuffer.wrap(input)).array();
120        }
121        /**
122         * {@link ShortBuffer} TO {@link ByteBuffer}
123         * @param input
124         * @return
125         */
126        public static ByteBuffer asByteBuffer(ShortBuffer input){
127                if(null == input ){
128                        return null;
129                }
130                ByteBuffer buffer = ByteBuffer.allocate(input.capacity()* (Short.SIZE/8));
131                while(input.hasRemaining()){
132                        buffer.putShort(input.get());
133                }
134                return buffer;
135        }
136        /**
137         * short[] TO byte[]
138         * @param input
139         * @return
140         */
141        public static byte[] asByteArray(short[] input){
142                if(null == input ){
143                        return null;
144                }
145                return asByteBuffer(ShortBuffer.wrap(input)).array();
146        }
147        /**
148         * byte[] TO double[]
149         * @param input
150         * @return
151         */
152        public static double[] asDoubleArray(byte[] input){
153                if(null == input ){
154                        return null;
155                }
156                DoubleBuffer buffer = ByteBuffer.wrap(input).asDoubleBuffer();
157                double[] res = new double[buffer.remaining()];
158                buffer.get(res);
159                return res;
160        }
161        /**
162         * byte[] TO float[]
163         * @param input
164         * @return
165         */
166        public static float[] asFloatArray(byte[] input){
167                if(null == input ){
168                        return null;
169                }
170                FloatBuffer buffer = ByteBuffer.wrap(input).asFloatBuffer();
171                float[] res = new float[buffer.remaining()];
172                buffer.get(res);
173                return res;
174        }
175        /**
176         * byte[] TO int[]
177         * @param input
178         * @return
179         */
180        public static int[] asIntArray(byte[] input){
181                if(null == input ){
182                        return null;
183                }
184                IntBuffer buffer = ByteBuffer.wrap(input).asIntBuffer();
185                int[] res = new int[buffer.remaining()];
186                buffer.get(res);
187                return res;
188        }
189        /**
190         * byte[] TO long[]
191         * @param input
192         * @return
193         */
194        public static long[] asLongArray(byte[] input){
195                if(null == input ){
196                        return null;
197                }
198                LongBuffer buffer = ByteBuffer.wrap(input).asLongBuffer();
199                long[] res = new long[buffer.remaining()];
200                buffer.get(res);
201                return res;
202        }
203        /**
204         * byte[] TO short[]
205         * @param input
206         * @return
207         */
208        public static short[] asShortArray(byte[] input){
209                if(null == input ){
210                        return null;
211                }
212                ShortBuffer buffer = ByteBuffer.wrap(input).asShortBuffer();
213                short[] res = new short[buffer.remaining()];
214                buffer.get(res);
215                return res;
216        }
217}