| 1 | /* |
| 2 | * $Id: NativeArrayOfStructs.java,v 1.5 2006/03/04 06:04:54 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | package com.moesol.bindings; |
| 13 | |
| 14 | import java.nio.ByteBuffer; |
| 15 | |
| 16 | /** |
| 17 | * Wrap native bytes that represent native arrays of structs. |
| 18 | * This class avoids making copies of the underlying bytes. |
| 19 | * |
| 20 | * @author Hastings |
| 21 | */ |
| 22 | public class NativeArrayOfStructs { |
| 23 | /** |
| 24 | * Create a new native array of structs on bb. |
| 25 | */ |
| 26 | public NativeArrayOfStructs(ByteBuffer bb) { |
| 27 | m_byte_buffer = bb; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Gets a slice of the array. Modifying the returned slice |
| 32 | * modifies the underlying structure as well. |
| 33 | * |
| 34 | * @param index zero based element index of slice start |
| 35 | * @param sizeof size in bytes of one element |
| 36 | * @return new slice for element |
| 37 | */ |
| 38 | protected ByteBuffer getElementSlice(int index, int sizeof) { |
| 39 | int offset = index * sizeof; |
| 40 | ByteBuffer dup = m_byte_buffer.duplicate(); |
| 41 | dup.position(offset); |
| 42 | ByteBuffer slice = dup.slice(); |
| 43 | slice.limit(sizeof); |
| 44 | return slice; |
| 45 | } |
| 46 | |
| 47 | public ByteBuffer _getByteBuffer() { |
| 48 | return m_byte_buffer; |
| 49 | } |
| 50 | |
| 51 | private ByteBuffer m_byte_buffer; // usually a non-direct byte buffer |
| 52 | } |