-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java: Add blocking direct binary encoder
- Loading branch information
Showing
6 changed files
with
244 additions
and
26 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
118 changes: 118 additions & 0 deletions
118
lang/java/avro/src/main/java/org/apache/avro/io/BlockingDirectBinaryEncoder.java
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,118 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* https://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 org.apache.avro.io; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* An {@link Encoder} for Avro's binary encoding that does not buffer output. | ||
* <p/> | ||
* This encoder does not buffer writes, and as a result is slower than | ||
* {@link BufferedBinaryEncoder}. However, it is lighter-weight and useful when | ||
* the buffering in BufferedBinaryEncoder is not desired and/or the Encoder is | ||
* very short-lived. | ||
* <p/> | ||
* To construct, use | ||
* {@link EncoderFactory#directBinaryEncoder(OutputStream, BinaryEncoder)} | ||
* <p/> | ||
* DirectBinaryEncoder is not thread-safe | ||
* | ||
* @see BinaryEncoder | ||
* @see EncoderFactory | ||
* @see Encoder | ||
* @see Decoder | ||
*/ | ||
public class BlockingDirectBinaryEncoder extends DirectBinaryEncoder { | ||
private OutputStream originalStream; | ||
|
||
private final ByteArrayOutputStream buffer; | ||
|
||
private boolean inBlock = false; | ||
|
||
private long blockItemCount; | ||
|
||
/** | ||
* Create a writer that sends its output to the underlying stream | ||
* <code>out</code>. | ||
* | ||
* @param out The Outputstream to write to | ||
*/ | ||
public BlockingDirectBinaryEncoder(OutputStream out) { | ||
super(out); | ||
buffer = new ByteArrayOutputStream(); | ||
} | ||
|
||
private void startBlock() { | ||
if (inBlock) { | ||
throw new RuntimeException("Nested Maps/Arrays are not supported by the BlockingDirectBinaryEncoder"); | ||
} | ||
originalStream = out; | ||
out = buffer; | ||
inBlock = true; | ||
} | ||
|
||
private void endBlock() { | ||
if (!inBlock) { | ||
throw new RuntimeException("Called endBlock, while not buffering a block"); | ||
} | ||
out = originalStream; | ||
if (blockItemCount > 0) { | ||
try { | ||
// Make it negative, so the reader knows that the number of bytes is coming | ||
writeLong(-blockItemCount); | ||
writeLong(buffer.size()); | ||
writeBytes(buffer.toByteArray()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
inBlock = false; | ||
buffer.reset(); | ||
} | ||
|
||
@Override | ||
public void setItemCount(long itemCount) throws IOException { | ||
blockItemCount = itemCount; | ||
} | ||
|
||
@Override | ||
public void writeArrayStart() throws IOException { | ||
startBlock(); | ||
} | ||
|
||
@Override | ||
public void writeArrayEnd() throws IOException { | ||
endBlock(); | ||
// Writes another zero to indicate that this is the last block | ||
super.writeArrayEnd(); | ||
} | ||
|
||
@Override | ||
public void writeMapStart() throws IOException { | ||
startBlock(); | ||
} | ||
|
||
@Override | ||
public void writeMapEnd() throws IOException { | ||
endBlock(); | ||
// Writes another zero to indicate that this is the last block | ||
super.writeMapEnd(); | ||
} | ||
} |
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.