View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
5    *               Henglin Wang <henglinwang@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 3 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, see
19   * <http://www.gnu.org/licenses/>.
20   */
21  package org.jumpmind.symmetric.web;
22  
23  import java.io.IOException;
24  import java.io.OutputStream;
25  
26  import javax.servlet.ServletOutputStream;
27  
28  import org.jumpmind.symmetric.util.MeteredOutputStream;
29  
30  public class ThrottledServletOutputStream extends ServletOutputStream {
31      private MeteredOutputStream stream;
32  
33      public ThrottledServletOutputStream(OutputStream output, long maxBps) {
34          stream = new MeteredOutputStream(output, maxBps);
35      }
36  
37      public ThrottledServletOutputStream(OutputStream output, long maxBps, long threshold) {
38          stream = new MeteredOutputStream(output, maxBps, threshold);
39      }
40  
41      public ThrottledServletOutputStream(OutputStream output, long maxBps, long threshold, long checkPoint) {
42          stream = new MeteredOutputStream(output, maxBps, threshold, checkPoint);
43      }
44  
45      @Override
46      public void write(int b) throws IOException {
47          stream.write(b);
48  
49      }
50  
51      public void write(byte[] b) throws IOException {
52          stream.write(b);
53      }
54  
55      public void write(byte[] b, int off, int len) throws IOException {
56          stream.write(b, off, len);
57      }
58  
59      @Override
60      public void close() throws IOException {
61  
62          super.close();
63          stream.close();
64      }
65  
66      @Override
67      public void flush() throws IOException {
68  
69          super.flush();
70          stream.flush();
71      }
72  
73  }