bufferwrapper.java
来自「world wind java sdk 源码」· Java 代码 · 共 676 行 · 第 1/2 页
JAVA
676 行
/*Copyright (C) 2001, 2008 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.util;import gov.nasa.worldwind.avlist.AVList;import gov.nasa.worldwind.avlist.AVKey;import java.nio.*;/** * @author tag * @version $Id: BufferWrapper.java 9704 2009-03-27 00:52:15Z tgaskins $ */public abstract class BufferWrapper{ public abstract int length(); public abstract int getInt(int index); public abstract double getDouble(int index); public abstract BufferWrapper getInt(int index, int[] dest, int destPos, int length); public abstract BufferWrapper getDouble(int index, double[] dest, int destPos, int length); public abstract BufferWrapper putInt(int index, int value); public abstract BufferWrapper putDouble(int index, double value); public abstract BufferWrapper putInt(int index, int[] src, int srcPos, int length); public abstract BufferWrapper putDouble(int index, double[] src, int srcPos, int length); public abstract long getSizeInBytes(); //**************************************************************// //******************** Primitive Array Wrappers **************// //**************************************************************// public static class IntArrayWrapper extends BufferWrapper { private int[] source; private int length; public IntArrayWrapper(int[] source, int length) { if (source == null) { String message = Logging.getMessage("nullValue.SourceIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.source = source; this.length = length; } public int length() { return this.length; } public int getInt(int index) { return this.source[index]; } public double getDouble(int index) { return this.source[index]; } public BufferWrapper getInt(int index, int[] dest, int destPos, int length) { System.arraycopy(this.source, index, dest, destPos, length); return this; } public BufferWrapper getDouble(int index, double[] dest, int destPos, int length) { for (int i = 0; i < length; i++) dest[destPos + i] = this.source[index + i]; return this; } public BufferWrapper putInt(int index, int value) { this.source[index] = value; return this; } public BufferWrapper putDouble(int index, double value) { this.source[index] = (int) value; return this; } public BufferWrapper putInt(int index, int[] src, int srcPos, int length) { System.arraycopy(src, srcPos, this.source, index, length); return this; } public BufferWrapper putDouble(int index, double[] src, int srcPos, int length) { for (int i = 0; i < length; i++) this.source[index + i] = (int) src[srcPos + i]; return this; } public long getSizeInBytes() { return (Integer.SIZE / 8) * this.length; } } //**************************************************************// //******************** NIO Buffer Wrappers *******************// //**************************************************************// public abstract static class NIOBufferWrapper extends BufferWrapper { private Buffer sourceBuffer; public NIOBufferWrapper(Buffer source) { if (source == null) { String message = Logging.getMessage("nullValue.SourceIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.sourceBuffer = source; this.sourceBuffer.rewind(); } public int length() { return this.sourceBuffer.limit(); } } public static class ByteBufferWrapper extends NIOBufferWrapper { private ByteBuffer byteBuffer; public ByteBufferWrapper(ByteBuffer source) { super(source); this.byteBuffer = source; } public int getInt(int index) { return this.byteBuffer.get(index); } public double getDouble(int index) { return this.byteBuffer.get(index); } public BufferWrapper getInt(int index, int[] dest, int destPos, int length) { if (dest == null) { String message = Logging.getMessage("nullValue.DestinationIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } byte[] buffer = new byte[length]; this.byteBuffer.position(index); this.byteBuffer.get(buffer, 0, length); this.byteBuffer.rewind(); for (int i = 0; i < length; i++) dest[i + destPos] = buffer[i]; return this; } public BufferWrapper getDouble(int index, double[] dest, int destPos, int length) { if (dest == null) { String message = Logging.getMessage("nullValue.DestinationIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } byte[] buffer = new byte[length]; this.byteBuffer.position(index); this.byteBuffer.get(buffer, 0, length); this.byteBuffer.rewind(); for (int i = 0; i < length; i++) dest[i + destPos] = buffer[i]; return this; } public BufferWrapper putInt(int index, int value) { this.byteBuffer.put(index, (byte) value); return this; } public BufferWrapper putDouble(int index, double value) { this.byteBuffer.put(index, (byte) value); return this; } public BufferWrapper putInt(int index, int[] src, int srcPos, int length) { if (src == null) { String message = Logging.getMessage("nullValue.SourceIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } byte[] buffer = new byte[length]; for (int i = 0; i < length; i++) buffer[i] = (byte) src[i + srcPos]; this.byteBuffer.position(index); this.byteBuffer.put(buffer, 0, length); this.byteBuffer.rewind(); return this; } public BufferWrapper putDouble(int index, double[] src, int srcPos, int length) { if (src == null) { String message = Logging.getMessage("nullValue.SourceIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } byte[] buffer = new byte[length]; for (int i = 0; i < length; i++) buffer[i] = (byte) src[i + srcPos]; this.byteBuffer.position(index); this.byteBuffer.put(buffer, 0, length); this.byteBuffer.rewind(); return this; } public long getSizeInBytes() { return this.byteBuffer.capacity(); } } public static class ShortBufferWrapper extends NIOBufferWrapper { private ShortBuffer shortBuffer; public ShortBufferWrapper(ShortBuffer source) { super(source); this.shortBuffer = source; } public int getInt(int index) { return this.shortBuffer.get(index); } public double getDouble(int index) { return this.shortBuffer.get(index); } public BufferWrapper getInt(int index, int[] dest, int destPos, int length) { if (dest == null) { String message = Logging.getMessage("nullValue.DestinationIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } short[] buffer = new short[length]; this.shortBuffer.position(index); this.shortBuffer.get(buffer, 0, length); this.shortBuffer.rewind(); for (int i = 0; i < length; i++) dest[i + destPos] = buffer[i]; return this; } public BufferWrapper getDouble(int index, double[] dest, int destPos, int length) { if (dest == null) { String message = Logging.getMessage("nullValue.DestinationIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } short[] buffer = new short[length]; this.shortBuffer.position(index); this.shortBuffer.get(buffer, 0, length); this.shortBuffer.rewind(); for (int i = 0; i < length; i++) dest[i + destPos] = buffer[i]; return this; } public BufferWrapper putInt(int index, int value) { this.shortBuffer.put(index, (short) value); return this; } public BufferWrapper putDouble(int index, double value) { this.shortBuffer.put(index, (short) value); return this; } public BufferWrapper putInt(int index, int[] src, int srcPos, int length) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?