mirror of https://github.com/hyperledger/besu
Remove unnecessary RlpInput and RlpOutput classes (#287)
parent
6125451acf
commit
ec1a8be558
@ -1,107 +0,0 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* 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 tech.pegasys.pantheon.ethereum.rlp; |
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument; |
||||
import static com.google.common.base.Preconditions.checkNotNull; |
||||
|
||||
import tech.pegasys.pantheon.util.bytes.Bytes32; |
||||
import tech.pegasys.pantheon.util.bytes.BytesValue; |
||||
import tech.pegasys.pantheon.util.bytes.BytesValues; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.math.BigInteger; |
||||
import java.nio.ByteBuffer; |
||||
import java.nio.channels.FileChannel; |
||||
|
||||
/** An {@link RLPInput} that reads RLP encoded data from a {@link File}. */ |
||||
public class FileRLPInput extends AbstractRLPInput { |
||||
|
||||
// The RLP encoded data.
|
||||
private final FileChannel file; |
||||
|
||||
public FileRLPInput(final FileChannel file, final boolean lenient) throws IOException { |
||||
super(lenient); |
||||
checkNotNull(file); |
||||
checkArgument(file.isOpen()); |
||||
this.file = file; |
||||
|
||||
init(file.size(), false); |
||||
} |
||||
|
||||
@Override |
||||
protected byte inputByte(final long offset) { |
||||
try { |
||||
final ByteBuffer buf = ByteBuffer.wrap(new byte[1]); |
||||
|
||||
file.read(buf, offset); |
||||
final byte b = buf.get(0); |
||||
return b; |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected BytesValue inputSlice(final long offset, final int length) { |
||||
try { |
||||
final byte[] bytes = new byte[length]; |
||||
final ByteBuffer buf = ByteBuffer.wrap(bytes); |
||||
file.read(buf, offset); |
||||
return BytesValue.of(bytes); |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Bytes32 inputSlice32(final long offset) { |
||||
return Bytes32.wrap(inputSlice(offset, 32), 0); |
||||
} |
||||
|
||||
@Override |
||||
protected String inputHex(final long offset, final int length) { |
||||
return inputSlice(offset, length).toString().substring(2); |
||||
} |
||||
|
||||
@Override |
||||
protected BigInteger getUnsignedBigInteger(final long offset, final int length) { |
||||
return BytesValues.asUnsignedBigInteger(inputSlice(offset, length)); |
||||
} |
||||
|
||||
@Override |
||||
protected int getInt(final long offset) { |
||||
return inputSlice(offset, Integer.BYTES).getInt(0); |
||||
} |
||||
|
||||
@Override |
||||
protected long getLong(final long offset) { |
||||
return inputSlice(offset, Long.BYTES).getLong(0); |
||||
} |
||||
|
||||
@Override |
||||
public BytesValue raw() { |
||||
throw new UnsupportedOperationException("raw() not supported on a Channel"); |
||||
} |
||||
|
||||
/** @return Offset of the current item */ |
||||
public long currentOffset() { |
||||
return currentItem; |
||||
} |
||||
|
||||
@Override |
||||
public void setTo(final long item) { |
||||
super.setTo(item); |
||||
} |
||||
} |
@ -1,96 +0,0 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* 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 tech.pegasys.pantheon.ethereum.rlp; |
||||
|
||||
import tech.pegasys.pantheon.util.bytes.Bytes32; |
||||
import tech.pegasys.pantheon.util.bytes.BytesValue; |
||||
import tech.pegasys.pantheon.util.bytes.BytesValues; |
||||
|
||||
import java.math.BigInteger; |
||||
|
||||
import io.vertx.core.buffer.Buffer; |
||||
|
||||
/** A {@link RLPInput} that decode RLP encoded data stored in a Vert.x {@link Buffer}. */ |
||||
public class VertxBufferRLPInput extends AbstractRLPInput { |
||||
|
||||
// The RLP encoded data.
|
||||
private final Buffer buffer; |
||||
// Offset in buffer from which to read.
|
||||
private final int bufferOffset; |
||||
|
||||
/** |
||||
* A new {@link RLPInput} that decodes data from the provided buffer. |
||||
* |
||||
* @param buffer The buffer from which to read RLP data. |
||||
* @param bufferOffset The offset in {@code buffer} in which the data to decode starts. |
||||
* @param lenient Whether the created decoded should be lenient, that is ignore non-fatal |
||||
* malformation in the input. |
||||
*/ |
||||
public VertxBufferRLPInput(final Buffer buffer, final int bufferOffset, final boolean lenient) { |
||||
super(lenient); |
||||
this.buffer = buffer; |
||||
this.bufferOffset = bufferOffset; |
||||
init(buffer.length(), false); |
||||
} |
||||
|
||||
/** |
||||
* The total size of the encoded data in the {@link Buffer} wrapped by this object. |
||||
* |
||||
* @return The total size of the encoded data that this input decodes (note that this value never |
||||
* changes, it is not the size of data remaining to decode, but the size to decode at creation |
||||
* time). |
||||
*/ |
||||
public int encodedSize() { |
||||
return Math.toIntExact(size); |
||||
} |
||||
|
||||
@Override |
||||
protected byte inputByte(final long offset) { |
||||
return buffer.getByte(Math.toIntExact(bufferOffset + offset)); |
||||
} |
||||
|
||||
@Override |
||||
protected BytesValue inputSlice(final long offset, final int length) { |
||||
return BytesValue.wrapBuffer(buffer, Math.toIntExact(bufferOffset + offset), length); |
||||
} |
||||
|
||||
@Override |
||||
protected Bytes32 inputSlice32(final long offset) { |
||||
return Bytes32.wrap(inputSlice(offset, Bytes32.SIZE), 0); |
||||
} |
||||
|
||||
@Override |
||||
protected String inputHex(final long offset, final int length) { |
||||
return inputSlice(offset, length).toString().substring(2); |
||||
} |
||||
|
||||
@Override |
||||
protected BigInteger getUnsignedBigInteger(final long offset, final int length) { |
||||
return BytesValues.asUnsignedBigInteger(inputSlice(offset, length)); |
||||
} |
||||
|
||||
@Override |
||||
protected int getInt(final long offset) { |
||||
return buffer.getInt(Math.toIntExact(bufferOffset + offset)); |
||||
} |
||||
|
||||
@Override |
||||
protected long getLong(final long offset) { |
||||
return buffer.getLong(Math.toIntExact(bufferOffset + offset)); |
||||
} |
||||
|
||||
@Override |
||||
public BytesValue raw() { |
||||
return BytesValue.wrap(buffer.getBytes()); |
||||
} |
||||
} |
@ -1,40 +0,0 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* 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 tech.pegasys.pantheon.ethereum.rlp; |
||||
|
||||
import tech.pegasys.pantheon.util.bytes.MutableBytesValue; |
||||
|
||||
import io.vertx.core.buffer.Buffer; |
||||
|
||||
/** |
||||
* A {@link RLPOutput} that writes/appends the result of RLP encoding to a Vert.x {@link Buffer}. |
||||
*/ |
||||
public class VertxBufferRLPOutput extends AbstractRLPOutput { |
||||
/** |
||||
* Appends the RLP-encoded data written to this output to the provided Vert.x {@link Buffer}. |
||||
* |
||||
* @param buffer The buffer to which to append the data to. |
||||
*/ |
||||
public void appendEncoded(final Buffer buffer) { |
||||
final int size = encodedSize(); |
||||
if (size == 0) { |
||||
return; |
||||
} |
||||
|
||||
// We want to append to the buffer, and Buffer always grows to accommodate anything writing,
|
||||
// so we write the last byte we know we'll need to make it resize accordingly.
|
||||
final int start = buffer.length(); |
||||
buffer.setByte(start + size - 1, (byte) 0); |
||||
writeEncoded(MutableBytesValue.wrapBuffer(buffer, start, size)); |
||||
} |
||||
} |
Loading…
Reference in new issue