File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed
main/java/com/azure/core/util
samples/java/com/azure/core/util
test/java/com/azure/core/util Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 1717import java .io .InputStream ;
1818import java .io .UncheckedIOException ;
1919import java .nio .ByteBuffer ;
20+ import java .nio .ReadOnlyBufferException ;
2021import java .nio .charset .Charset ;
2122import java .nio .charset .StandardCharsets ;
2223import java .util .Arrays ;
@@ -593,6 +594,21 @@ public InputStream toStream() {
593594 return new ByteArrayInputStream (this .data );
594595 }
595596
597+ /**
598+ * Returns a read-only {@link ByteBuffer} representation of this {@link BinaryData}.
599+ * <p>
600+ * Attempting to mutate the returned {@link ByteBuffer} will throw a {@link ReadOnlyBufferException}.
601+ *
602+ * <p><strong>Get a read-only ByteBuffer from the BinaryData</strong></p>
603+ *
604+ * {@codesnippet com.azure.util.BinaryData.toByteBuffer}
605+ *
606+ * @return A read-only {@link ByteBuffer} representing the {@link BinaryData}.
607+ */
608+ public ByteBuffer toByteBuffer () {
609+ return ByteBuffer .wrap (this .data ).asReadOnlyBuffer ();
610+ }
611+
596612 /* This will ensure lazy instantiation to avoid hard dependency on Json Serializer. */
597613 private static JsonSerializer getDefaultSerializer () {
598614 if (defaultJsonSerializer == null ) {
Original file line number Diff line number Diff line change @@ -656,6 +656,19 @@ public void toStream() throws IOException {
656656 // END: com.azure.core.util.BinaryData.toStream
657657 }
658658
659+ /**
660+ * Codesnippets for {@link BinaryData#toByteBuffer()}.
661+ */
662+ public void toReadOnlyByteBuffer () {
663+ // BEGIN: com.azure.util.BinaryData.toByteBuffer
664+ final byte [] data = "Some Data" .getBytes (StandardCharsets .UTF_8 );
665+ BinaryData binaryData = BinaryData .fromBytes (data );
666+ final byte [] bytes = new byte [data .length ];
667+ binaryData .toByteBuffer ().get (bytes , 0 , data .length );
668+ System .out .println (new String (bytes ));
669+ // END: com.azure.util.BinaryData.toByteBuffer
670+ }
671+
659672 public static class MyJsonSerializer implements JsonSerializer {
660673 private final ClientLogger logger = new ClientLogger (BinaryDataTest .MyJsonSerializer .class );
661674 private final ObjectMapper mapper ;
Original file line number Diff line number Diff line change 2121import java .io .OutputStream ;
2222import java .io .UncheckedIOException ;
2323import java .nio .ByteBuffer ;
24+ import java .nio .ReadOnlyBufferException ;
2425import java .nio .charset .StandardCharsets ;
2526import java .util .ArrayList ;
2627import java .util .List ;
2728
2829import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
2930import static org .junit .jupiter .api .Assertions .assertEquals ;
31+ import static org .junit .jupiter .api .Assertions .assertThrows ;
3032
3133/**
3234 * Test class for {@link BinaryData}.
@@ -289,6 +291,13 @@ public void createFromNullByte() {
289291 assertEquals ("" , data .toString ());
290292 }
291293
294+ @ Test
295+ public void toReadOnlyByteBufferThrowsOnMutation () {
296+ BinaryData binaryData = BinaryData .fromString ("Hello" );
297+
298+ assertThrows (ReadOnlyBufferException .class , () -> binaryData .toByteBuffer ().put ((byte ) 0 ));
299+ }
300+
292301 public static class MyJsonSerializer implements JsonSerializer {
293302 private final ClientLogger logger = new ClientLogger (MyJsonSerializer .class );
294303 private final ObjectMapper mapper ;
You can’t perform that action at this time.
0 commit comments