generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
336 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
serde-api/src/main/java/io/micronaut/serde/util/BinaryCodecUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.serde.util; | ||
|
||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.type.Argument; | ||
import io.micronaut.serde.Decoder; | ||
import io.micronaut.serde.Encoder; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Common implementations for reading/writing byte arrays. | ||
* | ||
* @since 2.1 | ||
* @author Jonas Konrad | ||
*/ | ||
@Internal | ||
public final class BinaryCodecUtil { | ||
private static final Argument<byte[]> BYTE_ARRAY = Argument.of(byte[].class); | ||
|
||
private BinaryCodecUtil() { | ||
} | ||
|
||
public static byte[] decodeFromArray(Decoder base) throws IOException { | ||
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
try (Decoder arrayDecoder = base.decodeArray(BYTE_ARRAY)) { | ||
while (arrayDecoder.hasNextArrayValue()) { | ||
Byte b = arrayDecoder.decodeByteNullable(); | ||
buffer.write(b == null ? 0 : b); | ||
} | ||
} | ||
return buffer.toByteArray(); | ||
} | ||
|
||
public static byte[] decodeFromBase64String(Decoder base) throws IOException { | ||
String s = base.decodeString(); | ||
try { | ||
return Base64.getDecoder().decode(s); | ||
} catch (IllegalArgumentException e) { | ||
throw base.createDeserializationException("Illegal base64 input: " + e.getMessage(), null); | ||
} | ||
} | ||
|
||
public static void encodeToArray(Encoder encoder, byte[] data) throws IOException { | ||
try (Encoder arrayEncoder = encoder.encodeArray(BYTE_ARRAY)) { | ||
for (byte i : data) { | ||
arrayEncoder.encodeByte(i); | ||
} | ||
} | ||
} | ||
|
||
public static void encodeToBase64String(Encoder encoder, byte[] data) throws IOException { | ||
encoder.encodeString(Base64.getEncoder().encodeToString(data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
serde-bson/src/test/java/io/micronaut/serde/bson/BinaryTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.micronaut.serde.bson; | ||
|
||
import io.micronaut.serde.annotation.Serdeable; | ||
|
||
@Serdeable | ||
public record BinaryTypes( | ||
byte[] uuid, | ||
byte[] normal, | ||
byte[] userDefined | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.