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

21
LINES

< > BotCompany Repo | #1036593 // limitedStruct - struct() but limited to a certain number of bytes

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

Libraryless. Click here for Pure Java version (12408L/70K).

sS limitedStruct(int limit, O o) {
  var baos = new ByteArrayOutputStream();
  var out = new LimitedOutputStream(baos, limit);
  var writer = printWriter(out);
  
  embedded void failMe() {
    fail("Struct exceeds limit of " + n2(limit) + " bytes: " + shorten(fromUtf8(baos.toByteArray()), limit));
  }
  
  try {
    structureToPrintWriter(o, writer);
  } catch e {
    writer.close();
    failMe();
  }
  writer.close();
  S struct = fromUtf8(baos.toByteArray());
  if (l(struct) > limit)
    failMe();
  ret struct;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036593
Snippet name: limitedStruct - struct() but limited to a certain number of bytes
Eternal ID of this version: #1036593/7
Text MD5: b1467923e855f5eb359d73490e2c5030
Transpilation MD5: 0828e72e8c63b2ef9b890e165c8476a4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-02 20:18:13
Source code size: 541 bytes / 21 lines
Pitched / IR pitched: No / No
Views / Downloads: 157 / 232
Version history: 6 change(s)
Referenced in: [show references]