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

37
LINES

< > BotCompany Repo | #1018464 // toByteArray

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11068L/61K).

1  
static byte[] toByteArray(ByteArrayOutputStream baos) {
2  
  ret baos == null ? null : baos.toByteArray();
3  
}
4  
5  
static byte[] toByteArray(Iterator<? extends Number> it) {
6  
  new ByteBuffer buf;
7  
  while (it.hasNext())
8  
    buf.add((byte) it.next().intValue());
9  
  ret buf.toByteArray();
10  
}
11  
12  
static byte[] toByteArray(Cl<? extends Number> it) {
13  
  int n = l(it), i = 0;
14  
  byte[] a = new[n];
15  
  for (x : it)
16  
    a[i++] = (byte) x.intValue();
17  
  ret a;
18  
}
19  
20  
static byte[] toByteArray(O o) {
21  
  if (o == null) null;
22  
  if (o cast byte[]) ret o;
23  
  if (o instanceof Iterator)
24  
    ret toByteArray((Iterator<Number>) o);
25  
  if (o cast Cl)
26  
    ret toByteArray((Cl<Number>) o);
27  
  // not sure what else to put here
28  
  fail("toByteArray", o);
29  
}
30  
31  
// Note: Does not rewind the buffer
32  
static byte[] toByteArray(java.nio.ByteBuffer buf) {
33  
  if (buf == null) null;
34  
  byte[] a = new[buf.remaining()];
35  
  buf.get(a);
36  
  ret a;
37  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, wnsclhtenguj

No comments. add comment

Snippet ID: #1018464
Snippet name: toByteArray
Eternal ID of this version: #1018464/10
Text MD5: f4b5d07fdae8e1bab67145cc36eedc13
Transpilation MD5: ef522ae7f6cc88ccf8613a87ad77a51e
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-09-03 13:56:24
Source code size: 924 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 348 / 493
Version history: 9 change(s)
Referenced in: [show references]