View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20
    21  package org.apache.mina.util;
    22
    23  import java.io.IOException;
    24  import java.io.InputStream;
    25  import java.nio.ByteBuffer;
    26
    27  import static org.junit.Assert.*;
    28  import org.junit.Test;
    29
    30  /**
    31   * A {@link ByteBufferInputStream} test.
    32   *
    33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    34   */
    35  public class ByteBufferInputStreamTest {
    36      @Test
    37      public void testEmpty() throws IOException {
    38          ByteBuffer bb = ByteBuffer.allocate(0);
    39          InputStream is = new ByteBufferInputStream(bb);
    40          assertEquals(-1, is.read());
    41          is.close();
    42      }
    43
    44      @Test
    45      public void testReadArray() throws IOException {
    46          byte[] src = "HELLO MINA".getBytes();
    47          byte[] dst = new byte[src.length];
    48          ByteBuffer bb = ByteBuffer.wrap(src);
    49          InputStream is = new ByteBufferInputStream(bb);
    50
    51          assertEquals(true, is.markSupported());
    52          is.mark(src.length);
    53          assertEquals(dst.length, is.read(dst));
    54          assertArrayEquals(src, dst);
    55          assertEquals(-1, is.read());
    56          is.close();
    57
    58          is.reset();
    59          byte[] dstTooBig = new byte[src.length + 1];
    60          assertEquals(src.length, is.read(dstTooBig));
    61
    62          assertEquals(-1, is.read(dstTooBig));
    63      }
    64
    65      @Test
    66      public void testSkip() throws IOException {
    67          byte[] src = "HELLO MINA!".getBytes();
    68          ByteBuffer bb = ByteBuffer.wrap(src);
    69          InputStream is = new ByteBufferInputStream(bb);
    70          is.skip(6);
    71
    72          assertEquals(5, is.available());
    73          assertEquals('M', is.read());
    74          assertEquals('I', is.read());
    75          assertEquals('N', is.read());
    76          assertEquals('A', is.read());
    77
    78          is.skip((long) Integer.MAX_VALUE + 1);
    79          assertEquals(-1, is.read());
    80          is.close();
    81      }
    82
    83  }