-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Inflater and Deflater symmetric (#1426)
I expect this to simplify adding DeflaterSink and InflaterSource.
- Loading branch information
1 parent
1d5f262
commit 260710e
Showing
5 changed files
with
128 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package okio | ||
|
||
private val emptyByteArray = byteArrayOf() | ||
|
||
/** | ||
* Transform a stream of source bytes into a stream of target bytes, one segment at a time. The | ||
* relationship between input byte count and output byte count is arbitrary: a sequence of input | ||
* bytes may produce zero output bytes, or many segments of output bytes. | ||
* | ||
* To use: | ||
* | ||
* 1. Create an instance. | ||
* | ||
* 2. Populate [source] with input data. Set [sourcePos] and [sourceLimit] to a readable slice of | ||
* this array. | ||
* | ||
* 3. Populate [target] with a destination for output data. Set [targetPos] and [targetLimit] to a | ||
* writable slice of this array. | ||
* | ||
* 4. Call [process] to read input data from [source] and write output to [target]. This function | ||
* advances [sourcePos] if input data was read and [targetPos] if compressed output was written. | ||
* If the input array is exhausted (`sourcePos == sourceLimit`) or the output array is full | ||
* (`targetPos == targetLimit`), make an adjustment and call [process] again. | ||
* | ||
* 5. Repeat steps 2 through 4 until the input data is completely exhausted. | ||
* | ||
* 6. Close the processor. | ||
* | ||
* See also, the [zlib manual](https://www.zlib.net/manual.html). | ||
*/ | ||
internal abstract class DataProcessor : Closeable { | ||
var source: ByteArray = emptyByteArray | ||
var sourcePos: Int = 0 | ||
var sourceLimit: Int = 0 | ||
|
||
var target: ByteArray = emptyByteArray | ||
var targetPos: Int = 0 | ||
var targetLimit: Int = 0 | ||
|
||
var closed: Boolean = false | ||
protected set | ||
|
||
/** | ||
* Returns true if no further calls to [process] are required to complete the operation. | ||
* Otherwise, make space available in [target] and call [process] again. | ||
*/ | ||
@Throws(ProtocolException::class) | ||
abstract fun process(): Boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.