Skip to content

Conversation

houzhizhen
Copy link
Contributor

No description provided.


private static final sun.misc.Unsafe UNSAFE;

private byte[] buffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set final

private static final sun.misc.Unsafe UNSAFE;

private byte[] buffer;
private int limit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set final

char c;
int count = 0;

/* use charAt instead of copying String to char array */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//

}
}
this.writeShort(utfLen);
this.require(utfLen);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert utfLen == bytes.length


public void skipBytes(int bytesToSkip) {
this.require(bytesToSkip);
this.position += bytesToSkip;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments used in what scenes

/**
* @return the internal byte array, can't modify the returned byte array
*/
public byte[] getByteArray() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buffer()

@Test
public void testShort() throws IOException {
UnsafeByteArrayGraphOutput output = new UnsafeByteArrayGraphOutput();
for (short i = -128; i <= 127; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more cases

@Test
public void testShortWithPosition() throws IOException {
UnsafeByteArrayGraphOutput output = new UnsafeByteArrayGraphOutput();
output.skipBytes(Constants.SHORT_LEN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return position

}

@Test
public void testGetByteArray() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename it

@houzhizhen houzhizhen force-pushed the unsafe branch 2 times, most recently from 6c0b9ba to 18da03a Compare February 3, 2021 04:07
public int skipBytes(int bytesToSkip) {
this.require(bytesToSkip);
int positionBeforeSkip = this.position;
this.position += bytesToSkip;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 spaces

output.writeInt(Integer.MAX_VALUE);
UnsafeByteArrayGraphInput input = new UnsafeByteArrayGraphInput(
output.getByteArray(),
output.buffer(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also update testGetByteArray


import sun.misc.Unsafe;

public class UnsafeByteArrayGraphInput implements GraphInput {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just implements DataInput

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set final

private static final int DEFAULT_SIZE = 32;

private byte[] buffer;
private int capacity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set final


import sun.misc.Unsafe;

public class UnsafeByteArrayGraphOutput implements GraphOutput {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set final

private static final sun.misc.Unsafe UNSAFE;

private final byte[] buffer;
private final int limit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set final

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already set final

public void writeShort(int v) {
this.require(Constants.SHORT_LEN);
UNSAFE.putShort(buffer,
Unsafe.ARRAY_BYTE_BASE_OFFSET + this.position,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align

return encoder.encode(CharBuffer.wrap(str.toCharArray()));
} catch (CharacterCodingException e) {
throw new ComputerException("Can't encode %s with UTF-8", e, str);
for (;i < strLen; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add space

output.write(i);
}
UnsafeByteArrayGraphInput input = new UnsafeByteArrayGraphInput(
output.toByteArray());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align with UnsafeByteArrayGraphInput

values[i] = ValueFactory.createValue(types[i].code());
output.writeValue(values[i]);
} else {
// Omit ValueType.UNKNOWN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert i == 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testWriteValue is removed

Value value = input.readValue();
Assert.assertEquals(values[i], value);
} else {
// Omitted ValueType.UNKNOWN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testWriteValue is removed


@Override
public Value readValue() throws IOException {
byte typeCode = this.readByte();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeCode => type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method readValue is removed

}

for (;i < strLen; i++) {
for ( ;i < strLen; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

";" first

UnsafeByteArrayOutput output = new UnsafeByteArrayOutput();
output.writeChars(chars);
UnsafeByteArrayGraphInput input = new UnsafeByteArrayGraphInput(
UnsafeByteArrayInput input = new UnsafeByteArrayInput(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update align

javeme
javeme previously approved these changes Feb 4, 2021

@Override
public int skipBytes(int n) {
int remaining = remaining();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.remaining()

* byte size and calls writeInt(int position, int v) to write the int at
* skipped position. The serialized byte size can be get by
* the difference of {@link #position()} before and after write the content.
* @returns the position before skip.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@returns ? seems => @return

@Override
public void writeBoolean(boolean v) {
this.require(Constants.BOOLEAN_LEN);
UNSAFE.putBoolean(buffer, this.offset(), v);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buffer => this.buffer, ditto as followers

}

public void writeShort(int position, int v) {
UNSAFE.putShort(buffer, this.offset(position), (short) v);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need require firstly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because it write at specified position

}

public void writeInt(int position, int v) {
this.require(Constants.INT_LEN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check position + Constants.INT_LEN < capacity

this.position += Constants.SHORT_LEN;
}

public void writeShort(int position, int v) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check position + Constants.SHORT_LEN < capacity

@houzhizhen houzhizhen merged commit 90a9656 into master Feb 4, 2021
@houzhizhen houzhizhen deleted the unsafe branch February 4, 2021 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants