Writing WebSocket Server on Java. Issue on Windows 7 64 bits - javascript

I having a problem with WebSocket protocol.
When I execute the code when develop (NetBeans) and works fine, even when I execute the jar file still works, but... When I execute the jar on Windows 7 64 bits platform... The WebSocket close by itself.
I'm working on local machine both the server and client, even I deactivate windows firewall and nothing changes.
Even I test with 3 browsers (Firefox, Chrome and Opera) and I fund the same result.
Any ideas?
Code of server listening thread
InputStream listen;
while (true) {
try {
if (socket.isClosed()) {
socket.close();
return;
} else {
listen = socket.getInputStream();
byte[] readData = new byte[1024];
int totalRead = listen.read(readData);
if (totalRead > 0) {
byte[] result = new byte[totalRead];
System.arraycopy(readData, 0, result, 0, totalRead);
String resultString = new String(result);
if (resultString.substring(0, 3).equals("GET")) {
HtmlRequest myRequest = new HtmlRequest(resultString);
caller.push(myRequest);
} else {
caller.push(WebsocketProtocol.decrypt(result, totalRead));
}
}
System.out.println(getName()+": total read -> "+totalRead);
}
}catch(SocketException e){
System.out.println(getName() + ": SocketException "+e.getMessage());
} catch (IOException ex) {
System.out.println(getName() + ": IOException "+ex.getMessage());
} catch (NullPointerException ex){
System.out.println(getName() + ": NullPointerException "+ex.getMessage());
} catch (Exception e) {
System.out.println(getName() + ": Exception "+e.getMessage());
}
}
Code of websocket protocol:
public static byte[] decrypt(byte[] leido, int largo) {
byte rLength = 0;
int rMaskIndex = 2;
int rDataStart = 0;
byte data = leido[1];
byte op = (byte) 127;
rLength = (byte) (data & op);
if (rLength == (byte) 126) {
rMaskIndex = 4;
}
if (rLength == (byte) 127) {
rMaskIndex = 10;
}
byte[] masks = new byte[4];
int j = 0;
int i = 0;
for (i = rMaskIndex; i < (rMaskIndex + 4); i++) {
masks[j] = leido[i];
j++;
}
rDataStart = rMaskIndex + 4;
int messLen = largo - rDataStart;
byte[] message = new byte[messLen];
for (i = rDataStart, j = 0; i < largo; i++, j++) {
message[j] = (byte) (leido[i] ^ masks[j % 4]);
}
return message;
}
public static byte[] encrypt(String message) {
byte[] bytesContent = message.getBytes();
int countFrames = 0;
byte[] frame = new byte[10];
frame[0] = (byte) 129;
if (bytesContent.length <= 125) {
frame[1] = (byte) bytesContent.length;
countFrames = 2;
} else if (bytesContent.length > 125 && bytesContent.length <= 65535) {
frame[1] = (byte) 126;
int largo = bytesContent.length;
frame[2] = (byte) ((largo >> 8) & (byte) 255);
frame[3] = (byte) (largo & (byte) 255);
countFrames = 4;
} else {
frame[1] = (byte) 127;
int largo = bytesContent.length;
frame[2] = (byte) ((largo >> 56) & (byte) 255);
frame[3] = (byte) ((largo >> 48) & (byte) 255);
frame[4] = (byte) ((largo >> 40) & (byte) 255);
frame[5] = (byte) ((largo >> 32) & (byte) 255);
frame[6] = (byte) ((largo >> 24) & (byte) 255);
frame[7] = (byte) ((largo >> 16) & (byte) 255);
frame[8] = (byte) ((largo >> 8) & (byte) 255);
frame[9] = (byte) (largo & (byte) 255);
countFrames = 10;
}
byte[] answer = new byte[countFrames + bytesContent.length];
int currentPosition = 0;
for (int i = 0; i < countFrames; i++) {
answer[currentPosition] = frame[i];
currentPosition++;
}
for (int i = 0; i < bytesContent.length; i++) {
answer[currentPosition] = bytesContent[i];
currentPosition++;
}
return answer;
}

Looks like the fail is about the default character set on JVM on windows 7.
In the call
call java -jar filename.jar
You have to add
call java -Dfile.encoding=UTF-8 -jar filename.jar

Related

How to run c++ and javascript client code (hackerrank style) on local machine

I am working on learning C++ and Javascript by solving Hackerrank problems. Right now C++ compiles but when I run it stalls. The same with my JavaScript. I would like to learn to feed in data for test cases like they do on the platform. For example:
If I have this code in C++:
#include <string>
#include <cstdio>
#include <iostream>
using namespace std;
vector<string> split_string(string);
// Complete the countApplesAndOranges function below.
void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
int count_a = 0;
int count_b = 0;
for (int i = 0; i < apples.size(); i++){
// apples[i] += a;
apples[i] += a;
if (s <= apples[i] && apples[i] <= t )
count_a +=1;
// cout << apples[i] << ;
}
// for (auto i: apples)
// // cout << i << ' ';
// cout << endl;
for (int i = 0; i < oranges.size(); i++){
// apples[i] += a;
oranges[i] += b;
if (s <= oranges[i] && oranges[i] <= t )
count_b +=1;
// cout << oranges[i] << "";
}
// for (auto i: oranges)
// // cout << i << ' ';
cout << count_a << endl;
cout << count_b;
}
int main()
{
string st_temp;
getline(cin, st_temp);
vector<string> st = split_string(st_temp);
int s = stoi(st[0]);
int t = stoi(st[1]);
string ab_temp;
getline(cin, ab_temp);
vector<string> ab = split_string(ab_temp);
int a = stoi(ab[0]);
int b = stoi(ab[1]);
string mn_temp;
getline(cin, mn_temp);
vector<string> mn = split_string(mn_temp);
int m = stoi(mn[0]);
int n = stoi(mn[1]);
string apples_temp_temp;
getline(cin, apples_temp_temp);
vector<string> apples_temp = split_string(apples_temp_temp);
vector<int> apples(m);
for (int i = 0; i < m; i++) {
int apples_item = stoi(apples_temp[i]);
apples[i] = apples_item;
}
string oranges_temp_temp;
getline(cin, oranges_temp_temp);
vector<string> oranges_temp = split_string(oranges_temp_temp);
vector<int> oranges(n);
for (int i = 0; i < n; i++) {
int oranges_item = stoi(oranges_temp[i]);
oranges[i] = oranges_item;
}
countApplesAndOranges(s, t, a, b, apples, oranges);
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
How would I go about "feeding" it inputs.
Similarly, for the JavaScript:
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
// Complete the countApplesAndOranges function below.
function countApplesAndOranges(s, t, a, b, apples, oranges) {
let count_a = 0
let count_b = 0
for (let i = 0; i < apples.length; i++){
apples[i] += a
if (s <= apples[i] && apples[i] <= t)
count_a += 1
}
for (let i = 0; i < oranges.length; i++){
oranges[i] += b
if (s <= oranges[i] && oranges[i] <= t)
count_b += 1
}
console.log(count_a)
console.log(count_b)
}
function main() {
const st = readLine().split(' ');
const s = parseInt(st[0], 10);
const t = parseInt(st[1], 10);
const ab = readLine().split(' ');
const a = parseInt(ab[0], 10);
const b = parseInt(ab[1], 10);
const mn = readLine().split(' ');
const m = parseInt(mn[0], 10);
const n = parseInt(mn[1], 10);
const apples = readLine().split(' ').map(applesTemp => parseInt(applesTemp, 10));
const oranges = readLine().split(' ').map(orangesTemp => parseInt(orangesTemp, 10));
countApplesAndOranges(s, t, a, b, apples, oranges);
}
I understand I could do this manually - by hardcoding local-variables to main() but I would like to learn about C++ - getline() function as well as JavaScript - process.stdin.
Thank you in advance for the help.
I figured out the C++, you have to add the input: eg.
7 11
5 15
3 2
-2 2 1
5 -6
It must be the exact number of lines as there are variables, or redirect the input from a file ie.
$ ./a.out < input.txt
For the JavaScript you must either:
Redirect the input:
$ node program.js < input.txt
or add lines and press to signify the end of input stream.
You can use pipeoperator to feed the input from a file
$ cat input.txt | node main.js
[ only works in Unix-based machine like MacBook, Linux, Ubuntu... ]
For the JavaScript, after typing the input into shell, you can press Ctrl + D to sends EOF(end-of-file) to trigger the event handler in process.stdin.on("end", ...) .
As mentioned in this post, in Microsoft Windows, the readline interface does not support ^D(Ctrl + D) by default

Equivalent JavaScript syntax

I am new to javascript and wondering what is the equivalent javascript syntax for below Java Code.
public static void crcValue(byte[] value) {
byte crc = 0;
for (int i = 0; i < 15; i++) {
crc = (byte) (value[i] + crc);
}
value[15] = (byte) (crc & 255);
}
I have tried the below code.
var cmd = new ArrayBuffer(16);
function crcValue(cmd){
var crc;
for (var i = 0; i < 15; i++) {
crc = cmd[i] + crc;
}
cmd[16] = crc & 255 ;
}

convert a text to hash in node js

I want to convert a text to hash id of numbers using node js. Already have a java program to convert but same kind of implementation to be done using node js.
Java Code
public static long generateId(String text) {
byte[] buffer = null;
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA1");
md.reset();
buffer = text.getBytes(Charsets.UTF_8);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
md.update(buffer);
byte[] digest = md.digest();
String hexStr = "";
for (int i = 0; i < digest.length; i++) {
hexStr += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
}
long hashid = 0;
for (int i = 0; i < hexStr.length(); i++)
hashid += Math.abs((long) Math.pow(27, 10 - i) * ('a' - (1 + hexStr.charAt(i))));
return hashid;
}
I was able to convert into nodejs upto digest after this I am unable to proceed.
function generateHashCode()
{
var text = '9/01/2017'+'xx'+'405'+''+'SDD'+'MDD'+'9';
var crypto = require('crypto');
console.log(crypto.createHash('SHA1').update(text).digest("hex"));
var hexDigest = crypto.createHash('SHA1').update(text).digest("hex");
var hexStr;
}
Kindly help me on this
function generateHashCode(text)
{
// assuming text is UTF-8 encoded
var crypto = require('crypto');
var hexDigest = crypto.createHash('SHA1').update(text).digest(); // this should be .digest() not .digest('hex')
var hexStr = "";
for (var i = 0; i < hexDigest.length; i++) {
hexStr += (((hexDigest[i] - 0x100) & 0xff) + 0x100).toString(16).substr(1); // fixed some math issues here
}
var hashid = 0;
var a = 'a'.charCodeAt(0); // or just var a = 97;
for (var i = 0; i < hexStr.length; i++)
hashid += Math.abs(Math.pow(27, 10 - i) * (a - (1 + hexStr.charCodeAt(i))));
return hashid;
}
console.log(generateHashCode("batman"));

How to create encrypted string with maximum 12 character from another long string

I have to create the bar code for retrieve the data back. Actual string maximum size is 60. But bar code i need to print is maximum 12 characters.
Can i encrypt long string to short and decrypt again to use in c# or javascript?
If your text only has ASCII characters, then you might be able to reduce it by half, by actually storing multiple ASCII characters in a single UTF8 character.
This would be the actual code:
public static class ByteExtensions
{
private const int BYTE_SIZE = 8;
public static byte[] Encode(this byte[] data)
{
if (data.Length == 0) return new byte[0];
int length = 3 * BYTE_SIZE;
BitArray source = new BitArray(data);
BitArray encoded = new BitArray(length);
int sourceBit = 0;
for (int i = (length / BYTE_SIZE); i > 1; i--)
{
for (int j = 6; j > 0; j--) encoded[i * BYTE_SIZE - 2 - j] = source[sourceBit++];
encoded[i * BYTE_SIZE - 1] = true;
encoded[i * BYTE_SIZE - 2] = false;
}
for (int i = BYTE_SIZE - 1; i > BYTE_SIZE - 1 - (length / BYTE_SIZE); i--) encoded[i] = true;
encoded[BYTE_SIZE - 1 - (length / BYTE_SIZE)] = false;
for (int i = 0; i <= BYTE_SIZE - 2 - (length / BYTE_SIZE); i++) encoded[i] = source[sourceBit++];
byte[] result = new byte[length / BYTE_SIZE];
encoded.CopyTo(result, 0);
return result;
}
public static byte[] Decode(this byte[] data)
{
if (data.Length == 0) return new byte[0];
int length = 2 * BYTE_SIZE;
BitArray source = new BitArray(data);
BitArray decoded = new BitArray(length);
int currentBit = 0;
for (int i = 3; i > 1; i--) for (int j = 6; j > 0; j--) decoded[currentBit++] = source[i * BYTE_SIZE - 2 - j];
for (int i = 0; i <= BYTE_SIZE - 5; i++) decoded[currentBit++] = source[i];
byte[] result = new byte[length / BYTE_SIZE];
decoded.CopyTo(result, 0);
return result;
}
}
public static class StringExtensions
{
public static string Encode(this string text)
{
byte[] ascii = Encoding.ASCII.GetBytes(text);
List<byte> encoded = new List<byte>();
for (int i = 0; i < ascii.Length; i += 2) encoded.AddRange(new byte[] { ascii[i], (i + 1) < ascii.Length ? ascii[i + 1] : (byte)30 }.Encode());
return Encoding.UTF8.GetString(encoded.ToArray());
}
public static string Decode(this string text)
{
byte[] utf8 = Encoding.UTF8.GetBytes(text);
List<byte> decoded = new List<byte>();
for (int i = 0; i < utf8.Length - 2; i += 3) decoded.AddRange(new byte[] { utf8[i], utf8[i + 1], utf8[i + 2] }.Decode());
return Encoding.ASCII.GetString(decoded.ToArray());
}
}
An example:
string text = "This is some large text which will be reduced by half!";
string encoded = text.Encode();
You won't be able to render it on a console window, since the text is now UTF8, but here is what encoded holds: 桔獩椠⁳潳敭氠牡敧琠硥⁴桷捩⁨楷汬戠⁥敲畤散⁤祢栠污Ⅶ
As you can see, we managed to encode a 54 characters long string into one with just 27 characters.
You can actually get back the original string by doing:
string decoded = encoded.Decode();

Uint8Array to string in Javascript

I have some UTF-8 encoded data living in a range of Uint8Array elements in Javascript. Is there an efficient way to decode these out to a regular javascript string (I believe Javascript uses 16 bit Unicode)? I dont want to add one character at the time as the string concaternation would become to CPU intensive.
TextEncoder and TextDecoder from the Encoding standard, which is polyfilled by the stringencoding library, converts between strings and ArrayBuffers:
var uint8array = new TextEncoder().encode("someString");
var string = new TextDecoder().decode(uint8array);
This should work:
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <iz#onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
It's somewhat cleaner as the other solutions because it doesn't use any hacks nor depends on Browser JS functions, e.g. works also in other JS environments.
Check out the JSFiddle demo.
Also see the related questions: here and here
Here's what I use:
var str = String.fromCharCode.apply(null, uint8Arr);
In Node "Buffer instances are also Uint8Array instances", so buf.toString() works in this case.
In NodeJS, we have Buffers available, and string conversion with them is really easy. Better, it's easy to convert a Uint8Array to a Buffer. Try this code, it's worked for me in Node for basically any conversion involving Uint8Arrays:
let str = Buffer.from(uint8arr.buffer).toString();
We're just extracting the ArrayBuffer from the Uint8Array and then converting that to a proper NodeJS Buffer. Then we convert the Buffer to a string (you can throw in a hex or base64 encoding if you want).
If we want to convert back to a Uint8Array from a string, then we'd do this:
let uint8arr = new Uint8Array(Buffer.from(str));
Be aware that if you declared an encoding like base64 when converting to a string, then you'd have to use Buffer.from(str, "base64") if you used base64, or whatever other encoding you used.
This will not work in the browser without a module! NodeJS Buffers just don't exist in the browser, so this method won't work unless you add Buffer functionality to the browser. That's actually pretty easy to do though, just use a module like this, which is both small and fast!
Found in one of the Chrome sample applications, although this is meant for larger blocks of data where you're okay with an asynchronous conversion.
/**
* Converts an array buffer to a string
*
* #private
* #param {ArrayBuffer} buf The buffer to convert
* #param {Function} callback The function to call when conversion is complete
*/
function _arrayBufferToString(buf, callback) {
var bb = new Blob([new Uint8Array(buf)]);
var f = new FileReader();
f.onload = function(e) {
callback(e.target.result);
};
f.readAsText(bb);
}
The solution given by Albert works well as long as the provided function is invoked infrequently and is only used for arrays of modest size, otherwise it is egregiously inefficient. Here is an enhanced vanilla JavaScript solution that works for both Node and browsers and has the following advantages:
• Works efficiently for all octet array sizes
• Generates no intermediate throw-away strings
• Supports 4-byte characters on modern JS engines (otherwise "?" is substituted)
var utf8ArrayToStr = (function () {
var charCache = new Array(128); // Preallocate the cache for the common single byte chars
var charFromCodePt = String.fromCodePoint || String.fromCharCode;
var result = [];
return function (array) {
var codePt, byte1;
var buffLen = array.length;
result.length = 0;
for (var i = 0; i < buffLen;) {
byte1 = array[i++];
if (byte1 <= 0x7F) {
codePt = byte1;
} else if (byte1 <= 0xDF) {
codePt = ((byte1 & 0x1F) << 6) | (array[i++] & 0x3F);
} else if (byte1 <= 0xEF) {
codePt = ((byte1 & 0x0F) << 12) | ((array[i++] & 0x3F) << 6) | (array[i++] & 0x3F);
} else if (String.fromCodePoint) {
codePt = ((byte1 & 0x07) << 18) | ((array[i++] & 0x3F) << 12) | ((array[i++] & 0x3F) << 6) | (array[i++] & 0x3F);
} else {
codePt = 63; // Cannot convert four byte code points, so use "?" instead
i += 3;
}
result.push(charCache[codePt] || (charCache[codePt] = charFromCodePt(codePt)));
}
return result.join('');
};
})();
Uint8Array to String
let str = Buffer.from(key.secretKey).toString('base64');
String to Uint8Array
let uint8arr = new Uint8Array(Buffer.from(data,'base64'));
I was frustrated to see that people were not showing how to go both ways or showing that things work on none trivial UTF8 strings. I found a post on codereview.stackexchange.com that has some code that works well. I used it to turn ancient runes into bytes, to test some crypo on the bytes, then convert things back into a string. The working code is on github here. I renamed the methods for clarity:
// https://codereview.stackexchange.com/a/3589/75693
function bytesToSring(bytes) {
var chars = [];
for(var i = 0, n = bytes.length; i < n;) {
chars.push(((bytes[i++] & 0xff) << 8) | (bytes[i++] & 0xff));
}
return String.fromCharCode.apply(null, chars);
}
// https://codereview.stackexchange.com/a/3589/75693
function stringToBytes(str) {
var bytes = [];
for(var i = 0, n = str.length; i < n; i++) {
var char = str.charCodeAt(i);
bytes.push(char >>> 8, char & 0xFF);
}
return bytes;
}
The unit test uses this UTF-8 string:
// http://kermitproject.org/utf8.html
// From the Anglo-Saxon Rune Poem (Rune version)
const secretUtf8 = `ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ
ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ
ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ᛬`;
Note that the string length is only 117 characters but the byte length, when encoded, is 234.
If I uncomment the console.log lines I can see that the string that is decoded is the same string that was encoded (with the bytes passed through Shamir's secret sharing algorithm!):
Do what #Sudhir said, and then to get a String out of the comma seperated list of numbers use:
for (var i=0; i<unitArr.byteLength; i++) {
myString += String.fromCharCode(unitArr[i])
}
This will give you the string you want,
if it's still relevant
If you can't use the TextDecoder API because it is not supported on IE:
You can use the FastestSmallestTextEncoderDecoder polyfill recommended by the Mozilla Developer Network website;
You can use this function also provided at the MDN website:
function utf8ArrayToString(aBytes) {
var sView = "";
for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
nPart = aBytes[nIdx];
sView += String.fromCharCode(
nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
/* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */
(nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
(nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
(nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
(nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
(nPart - 192 << 6) + aBytes[++nIdx] - 128
: /* nPart < 127 ? */ /* one byte */
nPart
);
}
return sView;
}
let str = utf8ArrayToString([50,72,226,130,130,32,43,32,79,226,130,130,32,226,135,140,32,50,72,226,130,130,79]);
// Must show 2H₂ + O₂ ⇌ 2H₂O
console.log(str);
Try these functions,
var JsonToArray = function(json)
{
var str = JSON.stringify(json, null, 0);
var ret = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
ret[i] = str.charCodeAt(i);
}
return ret
};
var binArrayToJson = function(binArray)
{
var str = "";
for (var i = 0; i < binArray.length; i++) {
str += String.fromCharCode(parseInt(binArray[i]));
}
return JSON.parse(str)
}
source: https://gist.github.com/tomfa/706d10fed78c497731ac, kudos to Tomfa
I'm using this function, which works for me:
function uint8ArrayToBase64(data) {
return btoa(Array.from(data).map((c) => String.fromCharCode(c)).join(''));
}
For ES6 and UTF8 string
decodeURIComponent(escape(String.fromCharCode(...uint8arrData)))
By far the easiest way that has worked for me is:
//1. Create or fetch the Uint8Array to use in the example
const bufferArray = new Uint8Array([10, 10, 10])
//2. Turn the Uint8Array into a regular array
const array = Array.from(bufferArray);
//3. Stringify it (option A)
JSON.stringify(array);
//3. Stringify it (option B: uses #serdarsenay code snippet to decode each item in array)
let binArrayToString = function(binArray) {
let str = "";
for (let i = 0; i < binArray.length; i++) {
str += String.fromCharCode(parseInt(binArray[i]));
}
return str;
}
binArrayToString(array);
class UTF8{
static encode(str:string){return new UTF8().encode(str)}
static decode(data:Uint8Array){return new UTF8().decode(data)}
private EOF_byte:number = -1;
private EOF_code_point:number = -1;
private encoderError(code_point) {
console.error("UTF8 encoderError",code_point)
}
private decoderError(fatal, opt_code_point?):number {
if (fatal) console.error("UTF8 decoderError",opt_code_point)
return opt_code_point || 0xFFFD;
}
private inRange(a:number, min:number, max:number) {
return min <= a && a <= max;
}
private div(n:number, d:number) {
return Math.floor(n / d);
}
private stringToCodePoints(string:string) {
/** #type {Array.<number>} */
let cps = [];
// Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
let i = 0, n = string.length;
while (i < string.length) {
let c = string.charCodeAt(i);
if (!this.inRange(c, 0xD800, 0xDFFF)) {
cps.push(c);
} else if (this.inRange(c, 0xDC00, 0xDFFF)) {
cps.push(0xFFFD);
} else { // (inRange(c, 0xD800, 0xDBFF))
if (i == n - 1) {
cps.push(0xFFFD);
} else {
let d = string.charCodeAt(i + 1);
if (this.inRange(d, 0xDC00, 0xDFFF)) {
let a = c & 0x3FF;
let b = d & 0x3FF;
i += 1;
cps.push(0x10000 + (a << 10) + b);
} else {
cps.push(0xFFFD);
}
}
}
i += 1;
}
return cps;
}
private encode(str:string):Uint8Array {
let pos:number = 0;
let codePoints = this.stringToCodePoints(str);
let outputBytes = [];
while (codePoints.length > pos) {
let code_point:number = codePoints[pos++];
if (this.inRange(code_point, 0xD800, 0xDFFF)) {
this.encoderError(code_point);
}
else if (this.inRange(code_point, 0x0000, 0x007f)) {
outputBytes.push(code_point);
} else {
let count = 0, offset = 0;
if (this.inRange(code_point, 0x0080, 0x07FF)) {
count = 1;
offset = 0xC0;
} else if (this.inRange(code_point, 0x0800, 0xFFFF)) {
count = 2;
offset = 0xE0;
} else if (this.inRange(code_point, 0x10000, 0x10FFFF)) {
count = 3;
offset = 0xF0;
}
outputBytes.push(this.div(code_point, Math.pow(64, count)) + offset);
while (count > 0) {
let temp = this.div(code_point, Math.pow(64, count - 1));
outputBytes.push(0x80 + (temp % 64));
count -= 1;
}
}
}
return new Uint8Array(outputBytes);
}
private decode(data:Uint8Array):string {
let fatal:boolean = false;
let pos:number = 0;
let result:string = "";
let code_point:number;
let utf8_code_point = 0;
let utf8_bytes_needed = 0;
let utf8_bytes_seen = 0;
let utf8_lower_boundary = 0;
while (data.length > pos) {
let _byte = data[pos++];
if (_byte == this.EOF_byte) {
if (utf8_bytes_needed != 0) {
code_point = this.decoderError(fatal);
} else {
code_point = this.EOF_code_point;
}
} else {
if (utf8_bytes_needed == 0) {
if (this.inRange(_byte, 0x00, 0x7F)) {
code_point = _byte;
} else {
if (this.inRange(_byte, 0xC2, 0xDF)) {
utf8_bytes_needed = 1;
utf8_lower_boundary = 0x80;
utf8_code_point = _byte - 0xC0;
} else if (this.inRange(_byte, 0xE0, 0xEF)) {
utf8_bytes_needed = 2;
utf8_lower_boundary = 0x800;
utf8_code_point = _byte - 0xE0;
} else if (this.inRange(_byte, 0xF0, 0xF4)) {
utf8_bytes_needed = 3;
utf8_lower_boundary = 0x10000;
utf8_code_point = _byte - 0xF0;
} else {
this.decoderError(fatal);
}
utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
code_point = null;
}
} else if (!this.inRange(_byte, 0x80, 0xBF)) {
utf8_code_point = 0;
utf8_bytes_needed = 0;
utf8_bytes_seen = 0;
utf8_lower_boundary = 0;
pos--;
code_point = this.decoderError(fatal, _byte);
} else {
utf8_bytes_seen += 1;
utf8_code_point = utf8_code_point + (_byte - 0x80) * Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
if (utf8_bytes_seen !== utf8_bytes_needed) {
code_point = null;
} else {
let cp = utf8_code_point;
let lower_boundary = utf8_lower_boundary;
utf8_code_point = 0;
utf8_bytes_needed = 0;
utf8_bytes_seen = 0;
utf8_lower_boundary = 0;
if (this.inRange(cp, lower_boundary, 0x10FFFF) && !this.inRange(cp, 0xD800, 0xDFFF)) {
code_point = cp;
} else {
code_point = this.decoderError(fatal, _byte);
}
}
}
}
//Decode string
if (code_point !== null && code_point !== this.EOF_code_point) {
if (code_point <= 0xFFFF) {
if (code_point > 0)result += String.fromCharCode(code_point);
} else {
code_point -= 0x10000;
result += String.fromCharCode(0xD800 + ((code_point >> 10) & 0x3ff));
result += String.fromCharCode(0xDC00 + (code_point & 0x3ff));
}
}
}
return result;
}
`
Using base64 as the encoding format works quite well. This is how it was implemented for passing secrets via urls in Firefox Send. You will need the base64-js package. These are the functions from the Send source code:
const b64 = require("base64-js")
function arrayToB64(array) {
return b64.fromByteArray(array).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "")
}
function b64ToArray(str) {
return b64.toByteArray(str + "===".slice((str.length + 3) % 4))
}
With vanilla, browser side, recording from microphone, base64 functions worked for me (I had to implement an audio sending function to a chat).
const ui8a = new Uint8Array(e.target.result);
const string = btoa(ui8a);
const ui8a_2 = atob(string).split(',');
Full code now. Thanks to Bryan Jennings & breakspirit#py4u.net for the code.
https://medium.com/#bryanjenningz/how-to-record-and-play-audio-in-javascript-faa1b2b3e49b
https://www.py4u.net/discuss/282499
index.html
<html>
<head>
<title>Record Audio Test</title>
<meta name="encoding" charset="utf-8" />
</head>
<body>
<h1>Audio Recording Test</h1>
<script src="index.js"></script>
<button id="action" onclick="start()">Start</button>
<button id="stop" onclick="stop()">Stop</button>
<button id="play" onclick="play()">Listen</button>
</body>
</html>
index.js:
const recordAudio = () =>
new Promise(async resolve => {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mediaRecorder = new MediaRecorder(stream);
const audioChunks = [];
mediaRecorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
const start = () => mediaRecorder.start();
const stop = () =>
new Promise(resolve => {
mediaRecorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks);
const audioUrl = URL.createObjectURL(audioBlob);
const audio = new Audio(audioUrl);
const play = () => audio.play();
resolve({ audioBlob, audioUrl, play });
});
mediaRecorder.stop();
});
resolve({ start, stop });
});
let recorder = null;
let audio = null;
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
const start = async () => {
recorder = await recordAudio();
recorder.start();
}
const stop = async () => {
audio = await recorder.stop();
read(audio.audioUrl);
}
const play = ()=> {
audio.play();
}
const read = (blobUrl)=> {
var xhr = new XMLHttpRequest;
xhr.responseType = 'blob';
xhr.onload = function() {
var recoveredBlob = xhr.response;
const reader = new FileReader();
// This fires after the blob has been read/loaded.
reader.addEventListener('loadend', (e) => {
const ui8a = new Uint8Array(e.target.result);
const string = btoa(ui8a);
const ui8a_2 = atob(string).split(',');
playByteArray(ui8a_2);
});
// Start reading the blob as text.
reader.readAsArrayBuffer(recoveredBlob);
};
// get the blob through blob url
xhr.open('GET', blobUrl);
xhr.send();
}
window.onload = init;
var context; // Audio context
var buf; // Audio buffer
function init() {
if (!window.AudioContext) {
if (!window.webkitAudioContext) {
alert("Your browser does not support any AudioContext and cannot play back this audio.");
return;
}
window.AudioContext = window.webkitAudioContext;
}
context = new AudioContext();
}
function playByteArray(byteArray) {
var arrayBuffer = new ArrayBuffer(byteArray.length);
var bufferView = new Uint8Array(arrayBuffer);
for (i = 0; i < byteArray.length; i++) {
bufferView[i] = byteArray[i];
}
context.decodeAudioData(arrayBuffer, function(buffer) {
buf = buffer;
play2();
});
}
// Play the loaded file
function play2() {
// Create a source node from the buffer
var source = context.createBufferSource();
source.buffer = buf;
// Connect to the final output node (the speakers)
source.connect(context.destination);
// Play immediately
source.start(0);
}
var decodedString = decodeURIComponent(escape(String.fromCharCode(...new Uint8Array(err))));
var obj = JSON.parse(decodedString);
I am using this Typescript snippet:
function UInt8ArrayToString(uInt8Array: Uint8Array): string
{
var s: string = "[";
for(var i: number = 0; i < uInt8Array.byteLength; i++)
{
if( i > 0 )
s += ", ";
s += uInt8Array[i];
}
s += "]";
return s;
}
Remove the type annotations if you need the JavaScript version.
Hope this helps!

Categories