1 /*
2 (C) Copyright IBM Corp. 2005, 2013
3
4 THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5 ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6 CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7
8 You can obtain a current copy of the Eclipse Public License from
9 http://www.opensource.org/licenses/eclipse-1.0.php
10
11 @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12 * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13 *
14 *
15 * Change History
16 * Flag Date Prog Description
17 *-------------------------------------------------------------------------------
18 * 1535756 2006-08-07 lupusalex Make code warning free
19 * 1565892 2006-11-28 lupusalex Make SBLIM client JSR48 compliant
20 * 2003590 2008-06-30 blaschke-oss Change licensing from CPL to EPL
21 * 2524131 2009-01-21 raman_arora Upgrade client to JDK 1.5 (Phase 1)
22 * 2531371 2009-02-10 raman_arora Upgrade client to JDK 1.5 (Phase 2)
23 * 2620 2013-02-23 blaschke-oss Chunked output broken
24 */
25 package org.metricshub.wbem.sblim.cimclient.internal.http.io;
26
27 /*-
28 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
29 * WBEM Java Client
30 * ჻჻჻჻჻჻
31 * Copyright 2023 - 2025 MetricsHub
32 * ჻჻჻჻჻჻
33 * Licensed under the Apache License, Version 2.0 (the "License");
34 * you may not use this file except in compliance with the License.
35 * You may obtain a copy of the License at
36 *
37 * http://www.apache.org/licenses/LICENSE-2.0
38 *
39 * Unless required by applicable law or agreed to in writing, software
40 * distributed under the License is distributed on an "AS IS" BASIS,
41 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 * See the License for the specific language governing permissions and
43 * limitations under the License.
44 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
45 */
46
47 import java.io.DataOutputStream;
48 import java.io.IOException;
49 import java.io.OutputStream;
50
51 /**
52 * Class ChunkedOutputStream implements an output stream for chunked messages
53 *
54 */
55 public class ChunkedOutputStream extends OutputStream {
56 DataOutputStream iOs;
57
58 byte[] iBuffer;
59
60 int iUsed;
61
62 /**
63 * Ctor.
64 *
65 * @param pStream
66 * The stream to create this one upon
67 * @param pBufferLength
68 * The buffer length. When this value is exceeded a new chunk
69 * will be started.
70 */
71 public ChunkedOutputStream(OutputStream pStream, int pBufferLength) {
72 this.iOs = new DataOutputStream(pStream);
73 this.iBuffer = new byte[pBufferLength];
74 this.iUsed = 0;
75 }
76
77 @Override
78 public void close() throws IOException {
79 flush();
80 this.iOs.writeBytes(Integer.toHexString(0) + "\r\n");
81 this.iOs.flush();
82 }
83
84 @Override
85 public void flush() throws IOException {
86 if (this.iUsed > 0) {
87 this.iOs.writeBytes(Integer.toHexString(this.iUsed) + "\r\n");
88 this.iOs.write(this.iBuffer, 0, this.iUsed);
89 this.iOs.writeBytes("\r\n");
90 this.iOs.flush();
91 }
92 this.iUsed = 0;
93 }
94
95 /**
96 * @param offset
97 */
98 @Override
99 public void write(byte source[], int offset, int len) throws IOException {
100 int copied = 0;
101 while (len > 0) {
102 int total = (this.iBuffer.length - this.iUsed < len) ? (this.iBuffer.length - this.iUsed) : len;
103 if (total > 0) {
104 System.arraycopy(source, copied, this.iBuffer, this.iUsed, total);
105 len -= total;
106 this.iUsed += total;
107 copied += total;
108 }
109 if (this.iUsed == this.iBuffer.length) flush();
110 }
111 }
112
113 @Override
114 public void write(int i) throws IOException {
115 if (this.iBuffer.length == this.iUsed) flush();
116 this.iBuffer[this.iUsed++] = (byte) (0xFF & i);
117 }
118 }