-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added an option to receive a base64 encoded vector as an input
- Loading branch information
Showing
2 changed files
with
54 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.liorkn.elasticsearch; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.DoubleBuffer; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Created by Lior Knaany on 4/7/18. | ||
*/ | ||
public class Util { | ||
|
||
public static final double[] convertBase64ToArray(String base64Str) { | ||
final byte[] decode = Base64.getDecoder().decode(base64Str.getBytes()); | ||
final DoubleBuffer doubleBuffer = ByteBuffer.wrap(decode).asDoubleBuffer(); | ||
|
||
final double[] dims = new double[doubleBuffer.capacity()]; | ||
doubleBuffer.get(dims); | ||
return dims; | ||
} | ||
|
||
public static final String convertArrayToBase64(double[] array) { | ||
final int capacity = 8 * array.length; | ||
final ByteBuffer bb = ByteBuffer.allocate(capacity); | ||
for (int i = 0; i < array.length; i++) { | ||
bb.putDouble(array[i]); | ||
} | ||
bb.rewind(); | ||
final ByteBuffer encodedBB = Base64.getEncoder().encode(bb); | ||
return new String(encodedBB.array()); | ||
} | ||
} |
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