# Create nd4j array backed by nio ByteBuffer

**URL:** <https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534>\
**Category:** ND4J\
**Created:** [August 6, 2021, 8:35am UTC](https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534 "2021-08-06T08:35:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MankaranSingh](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/mankaransingh/32/607_2.png) [@MankaranSingh](https://community.konduit.ai/u/MankaranSingh)\
**Post date:** [August 6, 2021, 8:35am UTC](https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534/1 "2021-08-06T08:35:04Z")

</div>

Hello ! I am using nd4j for cyclic workload, I have a nio ByteBuffer that contains all the data but the problem is I have to update the INDArray by creating a new one as `img_nd = Nd4j.create(imgYUVBuffer, YUVimgShape, DataType.FLOAT);`

What I want is the INDArray img\_nd get automatically updated on updating the ByteBuffer.

How can I achieve this ?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [August 6, 2021, 8:40am UTC](https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534/2 "2021-08-06T08:40:14Z")

</div>

@MankaranSingh you can create DataBuffers from ByteBuffers, but they have to be allocated using  
ByteBuffer.allocateDirect(…) or it will crash your JVM.

Something like:

```java
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(..);
DataBuffer buffer = Nd4j.createBuffer(byteBuffer,DataType.FLOAT,numberOfElementsInBuffer);
INDArray arr = Nd4j.create(buffer);

```

More here:

> <https://github.com/eclipse/deeplearning4j/blob/master/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java#L1372>

That should work for you.

---

<div class="post-metadata">

**Author:** ![MankaranSingh](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/mankaransingh/32/607_2.png) [@MankaranSingh](https://community.konduit.ai/u/MankaranSingh)\
**Post date:** [August 6, 2021, 1:59pm UTC](https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534/3 "2021-08-06T13:59:04Z")

</div>

Hi @agibsonccc. thanks for the reply ! This unfortunately didn’t work out for me. Updating the ByteBuffer dosent reflecrs changes on the ndarray. Could this be because my data is in float and the ndarray buffer takes in byte buffer ? The method you mentioned says that `This will wrap the buffer as a reference (no copy) if the allocation opType is the same.`

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [August 6, 2021, 2:14pm UTC](https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534/4 "2021-08-06T14:14:55Z")

</div>

@MankaranSingh Could you give me something end to end reproducible I can just run on my own to see what you’re talking about? I would need to see how you’re creating your bytebuffer and how you’re testing everything.

---

<div class="post-metadata">

**Author:** ![MankaranSingh](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/mankaransingh/32/607_2.png) [@MankaranSingh](https://community.konduit.ai/u/MankaranSingh)\
**Post date:** [August 6, 2021, 2:32pm UTC](https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534/5 "2021-08-06T14:32:45Z")

</div>

Hi @agibsonccc, I used the exact same example as you metioned.

But I think I fixed the issue. what I did was,

```auto
    float imgYUVBuffer[] = new float[512*384*1];
    long[] YUVimgShape = {384, 512, 1};
    DataBuffer imgYUVDataBuffer = Nd4j.createBuffer(imgYUVBuffer);
    INDArray img_nd = Nd4j.create(imgYUVDataBuffer, YUVimgShape);
    FloatBuffer imgYUVByteBuffer = imgYUVDataBuffer.asNioFloat();

```

now editing imgYUVByteBuffer directly edits the ndarray. I know this is a bit of a hack but it worked.

If you’d like, I will submit the code that didn’t work in a bit after creating a minimal runnable script.

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [August 6, 2021, 11:30pm UTC](https://community.konduit.ai/t/create-nd4j-array-backed-by-nio-bytebuffer/1534/6 "2021-08-06T23:30:45Z")

</div>

> [@MankaranSingh](#):
>
> ```auto
> DataBuffer imgYUVDataBuffer = Nd4j.createBuffer(imgYUVBuffer);
> 
> ```

@MankaranSingh I would need how you created the original bytebuffer as well. That matters a lot. You gave me the ByteBuffer already existing. On heap buffers won’t work with nd4j.
