2626import net .imglib2 .util .Cast ;
2727
2828import java .nio .ByteBuffer ;
29- import java .nio .DoubleBuffer ;
30- import java .nio .FloatBuffer ;
31- import java .nio .IntBuffer ;
32- import java .nio .LongBuffer ;
3329import java .util .Arrays ;
3430
3531import org .tensorflow .Tensor ;
@@ -102,7 +98,10 @@ private static TUint8 buildUByte(SharedMemoryArray tensor)
10298 if (!tensor .isNumpyFormat ())
10399 throw new IllegalArgumentException ("Shared memory arrays must be saved in numpy format." );
104100 ByteBuffer buff = tensor .getDataBufferNoHeader ();
105- ByteDataBuffer dataBuffer = RawDataBufferFactory .create (buff .array (), false );
101+ byte [] flat = new byte [buff .capacity ()];
102+ buff .get (flat );
103+ buff .rewind ();
104+ ByteDataBuffer dataBuffer = RawDataBufferFactory .create (flat , false );
106105 TUint8 ndarray = Tensor .of (TUint8 .class , Shape .of (ogShape ), dataBuffer );
107106 return ndarray ;
108107 }
@@ -117,8 +116,10 @@ private static TInt32 buildInt(SharedMemoryArray tensor)
117116 if (!tensor .isNumpyFormat ())
118117 throw new IllegalArgumentException ("Shared memory arrays must be saved in numpy format." );
119118 ByteBuffer buff = tensor .getDataBufferNoHeader ();
120- IntBuffer intBuff = buff .asIntBuffer ();
121- IntDataBuffer dataBuffer = RawDataBufferFactory .create (intBuff .array (), false );
119+ int [] flat = new int [buff .capacity () / 4 ];
120+ buff .asIntBuffer ().get (flat );
121+ buff .rewind ();
122+ IntDataBuffer dataBuffer = RawDataBufferFactory .create (flat , false );
122123 TInt32 ndarray = TInt32 .tensorOf (Shape .of (ogShape ),
123124 dataBuffer );
124125 return ndarray ;
@@ -134,8 +135,10 @@ private static TInt64 buildLong(SharedMemoryArray tensor)
134135 if (!tensor .isNumpyFormat ())
135136 throw new IllegalArgumentException ("Shared memory arrays must be saved in numpy format." );
136137 ByteBuffer buff = tensor .getDataBufferNoHeader ();
137- LongBuffer longBuff = buff .asLongBuffer ();
138- LongDataBuffer dataBuffer = RawDataBufferFactory .create (longBuff .array (), false );
138+ long [] flat = new long [buff .capacity () / 8 ];
139+ buff .asLongBuffer ().get (flat );
140+ buff .rewind ();
141+ LongDataBuffer dataBuffer = RawDataBufferFactory .create (flat , false );
139142 TInt64 ndarray = TInt64 .tensorOf (Shape .of (ogShape ),
140143 dataBuffer );
141144 return ndarray ;
@@ -151,8 +154,10 @@ private static TFloat32 buildFloat(SharedMemoryArray tensor)
151154 if (!tensor .isNumpyFormat ())
152155 throw new IllegalArgumentException ("Shared memory arrays must be saved in numpy format." );
153156 ByteBuffer buff = tensor .getDataBufferNoHeader ();
154- FloatBuffer floatBuff = buff .asFloatBuffer ();
155- FloatDataBuffer dataBuffer = RawDataBufferFactory .create (floatBuff .array (), false );
157+ float [] flat = new float [buff .capacity () / 4 ];
158+ buff .asFloatBuffer ().get (flat );
159+ buff .rewind ();
160+ FloatDataBuffer dataBuffer = RawDataBufferFactory .create (flat , false );
156161 TFloat32 ndarray = TFloat32 .tensorOf (Shape .of (ogShape ), dataBuffer );
157162 return ndarray ;
158163 }
@@ -167,8 +172,10 @@ private static TFloat64 buildDouble(SharedMemoryArray tensor)
167172 if (!tensor .isNumpyFormat ())
168173 throw new IllegalArgumentException ("Shared memory arrays must be saved in numpy format." );
169174 ByteBuffer buff = tensor .getDataBufferNoHeader ();
170- DoubleBuffer floatBuff = buff .asDoubleBuffer ();
171- DoubleDataBuffer dataBuffer = RawDataBufferFactory .create (floatBuff .array (), false );
175+ double [] flat = new double [buff .capacity () / 8 ];
176+ buff .asDoubleBuffer ().get (flat );
177+ buff .rewind ();
178+ DoubleDataBuffer dataBuffer = RawDataBufferFactory .create (flat , false );
172179 TFloat64 ndarray = TFloat64 .tensorOf (Shape .of (ogShape ), dataBuffer );
173180 return ndarray ;
174181 }
0 commit comments