Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

23
LINES

< > BotCompany Repo | #1008076 // UTF8Processor - read an UTF-8 stream char by char

JavaX fragment (include)

1  
sclass UTF8Processor {
2  
  byte[] buffer = new byte[5];
3  
  int count = 0;
4  
5  
  S processByte(byte nextByte) ctex {
6  
    buffer[count++] = nextByte;
7  
    if(count == expectedBytes()) {
8  
        S result = new S(buffer, 0, count, "UTF-8");
9  
        count = 0;
10  
        return result;
11  
    }
12  
    return "";
13  
  }
14  
15  
  int expectedBytes() {
16  
    int num = buffer[0] & 255;
17  
    if(num < 0x80) return 1;
18  
    if(num < 0xe0) return 2;
19  
    if(num < 0xf0) return 3;
20  
    if(num < 0xf8) return 4;
21  
    return 5;
22  
  }
23  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008076
Snippet name: UTF8Processor - read an UTF-8 stream char by char
Eternal ID of this version: #1008076/2
Text MD5: 8ba20377624bfc731e1503430bb3d864
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-23 16:27:22
Source code size: 510 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 503 / 1446
Version history: 1 change(s)
Referenced in: [show references]