]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/transport/TZlibTransport.java
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 package org
.apache
.thrift
.transport
;
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
;
30 * TZlibTransport deflates on write and inflates on read.
32 public class TZlibTransport
extends TIOStreamTransport
{
34 private TTransport transport_
= null;
36 public static class Factory
extends TTransportFactory
{
41 public TTransport
getTransport(TTransport base
) {
42 return new TZlibTransport(base
);
47 * Constructs a new TZlibTransport instance.
48 * @param transport the underlying transport to read from and write to
50 public TZlibTransport(TTransport transport
) {
51 this(transport
, Deflater
.BEST_COMPRESSION
);
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
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);
66 public boolean isOpen() {
67 return transport_
.isOpen();
71 public void open() throws TTransportException
{
78 if (transport_
.isOpen()) {
84 class TTransportInputStream
extends InputStream
{
86 private TTransport transport
= null;
88 public TTransportInputStream(TTransport transport
) {
89 this.transport
= transport
;
93 public int read() throws IOException
{
95 byte[] buf
= new byte[1];
96 transport
.read(buf
, 0, 1);
98 } catch (TTransportException e
) {
99 throw new IOException(e
);
104 public int read(byte b
[], int off
, int len
) throws IOException
{
106 return transport
.read(b
, off
, len
);
107 } catch (TTransportException e
) {
108 throw new IOException(e
);
113 class TTransportOutputStream
extends OutputStream
{
115 private TTransport transport
= null;
117 public TTransportOutputStream(TTransport transport
) {
118 this.transport
= transport
;
122 public void write(final int b
) throws IOException
{
124 transport
.write(new byte[]{(byte) b
});
125 } catch (TTransportException e
) {
126 throw new IOException(e
);
131 public void write(byte b
[], int off
, int len
) throws IOException
{
133 transport
.write(b
, off
, len
);
134 } catch (TTransportException e
) {
135 throw new IOException(e
);
140 public void flush() throws IOException
{
143 } catch (TTransportException e
) {
144 throw new IOException(e
);