Skip to content

Commit

Permalink
Merge pull request #94 from codewise/master
Browse files Browse the repository at this point in the history
[Java] DirectBufferInputStream is missing 0xFF byte mask in read()
  • Loading branch information
mjpt777 authored Feb 24, 2017
2 parents 2babf8b + d57856f commit 9ef8b81
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public int read() throws IOException
int b = -1;
if (position < length)
{
b = buffer.getByte(offset + position);
b = buffer.getByte(offset + position) & 0xFF;
++position;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2014 - 2015 Real Logic Ltd.
*
* 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 org.agrona.io;

import org.agrona.DirectBuffer;
import org.agrona.concurrent.UnsafeBuffer;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class DirectBufferInputStreamTest
{

private static final int END_OF_STREAM_MARKER = -1;

@Test
public void shouldCorrectlyConvertBytesToPositiveIntegers() throws IOException
{
final byte[] data = {(byte) -1, 0};
final DirectBuffer buffer = new UnsafeBuffer(data);
final DirectBufferInputStream inputStream = new DirectBufferInputStream(buffer);

assertEquals(inputStream.read(), 255);
}

@Test
public void shouldReturnMinusOneOnEndOfStream() throws IOException
{
final byte[] data = {1, 2};

final DirectBuffer buffer = new UnsafeBuffer(data);
final DirectBufferInputStream inputStream = new DirectBufferInputStream(buffer);

assertEquals(inputStream.read(), 1);
assertEquals(inputStream.read(), 2);
assertEquals(inputStream.read(), END_OF_STREAM_MARKER);
}
}

0 comments on commit 9ef8b81

Please sign in to comment.