]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/transport/TZlibTransport.java
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / src / org / apache / thrift / transport / TZlibTransport.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.thrift.transport;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.zip.Deflater;
25 import java.util.zip.DeflaterOutputStream;
26 import java.util.zip.Inflater;
27 import java.util.zip.InflaterInputStream;
28
29 /**
30 * TZlibTransport deflates on write and inflates on read.
31 */
32 public class TZlibTransport extends TIOStreamTransport {
33
34 private TTransport transport_ = null;
35
36 public static class Factory extends TTransportFactory {
37 public Factory() {
38 }
39
40 @Override
41 public TTransport getTransport(TTransport base) {
42 return new TZlibTransport(base);
43 }
44 }
45
46 /**
47 * Constructs a new TZlibTransport instance.
48 * @param transport the underlying transport to read from and write to
49 */
50 public TZlibTransport(TTransport transport) {
51 this(transport, Deflater.BEST_COMPRESSION);
52 }
53
54 /**
55 * Constructs a new TZlibTransport instance.
56 * @param transport the underlying transport to read from and write to
57 * @param compressionLevel 0 for no compression, 9 for maximum compression
58 */
59 public TZlibTransport(TTransport transport, int compressionLevel) {
60 transport_ = transport;
61 inputStream_ = new InflaterInputStream(new TTransportInputStream(transport_), new Inflater());
62 outputStream_ = new DeflaterOutputStream(new TTransportOutputStream(transport_), new Deflater(compressionLevel, false), true);
63 }
64
65 @Override
66 public boolean isOpen() {
67 return transport_.isOpen();
68 }
69
70 @Override
71 public void open() throws TTransportException {
72 transport_.open();
73 }
74
75 @Override
76 public void close() {
77 super.close();
78 if (transport_.isOpen()) {
79 transport_.close();
80 }
81 }
82 }
83
84 class TTransportInputStream extends InputStream {
85
86 private TTransport transport = null;
87
88 public TTransportInputStream(TTransport transport) {
89 this.transport = transport;
90 }
91
92 @Override
93 public int read() throws IOException {
94 try {
95 byte[] buf = new byte[1];
96 transport.read(buf, 0, 1);
97 return buf[0];
98 } catch (TTransportException e) {
99 throw new IOException(e);
100 }
101 }
102
103 @Override
104 public int read(byte b[], int off, int len) throws IOException {
105 try {
106 return transport.read(b, off, len);
107 } catch (TTransportException e) {
108 throw new IOException(e);
109 }
110 }
111 }
112
113 class TTransportOutputStream extends OutputStream {
114
115 private TTransport transport = null;
116
117 public TTransportOutputStream(TTransport transport) {
118 this.transport = transport;
119 }
120
121 @Override
122 public void write(final int b) throws IOException {
123 try {
124 transport.write(new byte[]{(byte) b});
125 } catch (TTransportException e) {
126 throw new IOException(e);
127 }
128 }
129
130 @Override
131 public void write(byte b[], int off, int len) throws IOException {
132 try {
133 transport.write(b, off, len);
134 } catch (TTransportException e) {
135 throw new IOException(e);
136 }
137 }
138
139 @Override
140 public void flush() throws IOException {
141 try {
142 transport.flush();
143 } catch (TTransportException e) {
144 throw new IOException(e);
145 }
146 }
147 }
148