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

29
LINES

< > BotCompany Repo | #1036592 // LimitedOutputStream - OutputStream that fails when writing too much

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

Libraryless. Click here for Pure Java version (10729L/59K).

1  
sclass LimitedOutputStream extends CountingOutputStream {
2  
  long limit;
3  
  
4  
  *(OutputStream out) { super(out); }
5  
  *(OutputStream out, long *limit) { super(out); }
6  
  
7  
  @Override
8  
  public void write(int b) throws IOException {
9  
    checkLimit();
10  
    super.write(b);
11  
  }
12  
13  
  @Override
14  
  public void write(byte[] b) throws IOException {
15  
    checkLimit();
16  
    super.write(b);
17  
  }
18  
19  
  @Override
20  
  public void write(byte[] b, int off, int len) throws IOException {
21  
    checkLimit();
22  
    super.write(b, off, len);
23  
  }
24  
  
25  
  void checkLimit {
26  
    if (counter >= limit)
27  
      fail("Write limit reached (" + n2(limit) + " bytes)");
28  
  }
29  
}

Author comment

Began life as a copy of #1029366

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036592
Snippet name: LimitedOutputStream - OutputStream that fails when writing too much
Eternal ID of this version: #1036592/3
Text MD5: 34f1141d6c9a0a0f526b57a332e8adce
Transpilation MD5: b9bccc7ba4b7d1efa70ddc67f3552b1b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-02 18:22:30
Source code size: 652 bytes / 29 lines
Pitched / IR pitched: No / No
Views / Downloads: 174 / 223
Version history: 2 change(s)
Referenced in: [show references]