File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\ByteBuffer.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import java.io.OutputStream;
27 import java.io.IOException;
28 import java.io.ByteArrayOutputStream;
29 import java.io.InputStream;
30
31 /**
32 * {@link ByteArrayOutputStream} re-implementation.
33 *
34 * <p>
35 * This version allows one to read while writing is in progress.
36 *
37 * @author Kohsuke Kawaguchi
38 * @deprecated since 2008-05-28. Moved to stapler
39 */
40 public class ByteBuffer extends OutputStream {
41 private byte[] buf = new byte[8192];
42 /**
43 * Size of the data.
44 */
45 private int size = 0;
46
47
48 public synchronized void write(byte b[], int off, int len) throws IOException {
49 ensureCapacity(len);
50 System.arraycopy(b,off,buf,size,len);
51 size+=len;
52 }
53
54 public synchronized void write(int b) throws IOException {
55 ensureCapacity(1);
56 buf[size++] = (byte)b;
57 }
58
59 public synchronized long length() {
60 return size;
61 }
62
63 private void ensureCapacity(int len) {
64 if(buf.length-size>len)
65 return;
66
67 byte[] n = new byte[Math.max(buf.length*2, size+len)];
68 System.arraycopy(buf,0,n,0,size);
69 this.buf = n;
70 }
71
72 public synchronized String toString() {
73 return new String(buf,0,size);
74 }
75
76 /**
77 * Writes the contents of this buffer to another OutputStream.
78 */
79 public synchronized void writeTo(OutputStream os) throws IOException {
80 os.write(buf,0,size);
81 }
82
83 /**
84 * Creates an {@link InputStream} that reads from the underlying buffer.
85 */
86 public InputStream newInputStream() {
87 return new InputStream() {
88 private int pos = 0;
89 public int read() throws IOException {
90 synchronized(ByteBuffer.this) {
91 if(pos>=size) return -1;
92 return buf[pos++];
93 }
94 }
95
96 public int read(byte b[], int off, int len) throws IOException {
97 synchronized(ByteBuffer.this) {
98 if(size==pos)
99 return -1;
100
101 int sz = Math.min(len,size-pos);
102 System.arraycopy(buf,pos,b,off,sz);
103 pos+=sz;
104 return sz;
105 }
106 }
107
108
109 public int available() throws IOException {
110 synchronized(ByteBuffer.this) {
111 return size-pos;
112 }
113 }
114
115 public long skip(long n) throws IOException {
116 synchronized(ByteBuffer.this) {
117 int diff = (int) Math.min(n,size-pos);
118 pos+=diff;
119 return diff;
120 }
121 }
122 };
123 }
124 }
125