tests/libjava-mauve/src/gnu/testlet/java/nio/channels/FileChannel/offsetSingleBuffer.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 /* offsetSingleBuffer.java -- Test writing offset from a single buffer
       
     2    Copyright (C) 2006 Michael Barker
       
     3 This file is part of Mauve.
       
     4 
       
     5 Mauve is free software; you can redistribute it and/or modify
       
     6 it under the terms of the GNU General Public License as published by
       
     7 the Free Software Foundation; either version 2, or (at your option)
       
     8 any later version.
       
     9 
       
    10 Mauve is distributed in the hope that it will be useful, but
       
    11 WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13 General Public License for more details.
       
    14 
       
    15 You should have received a copy of the GNU General Public License
       
    16 along with Mauve; see the file COPYING.  If not, write to the
       
    17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
       
    18 02110-1301 USA.
       
    19 
       
    20 */
       
    21 
       
    22 // Tags: JDK14
       
    23 
       
    24 package gnu.testlet.java.nio.channels.FileChannel;
       
    25 
       
    26 import gnu.testlet.TestHarness;
       
    27 import gnu.testlet.Testlet;
       
    28 
       
    29 import java.io.File;
       
    30 import java.io.FileInputStream;
       
    31 import java.io.FileNotFoundException;
       
    32 import java.io.FileOutputStream;
       
    33 import java.io.IOException;
       
    34 import java.io.UnsupportedEncodingException;
       
    35 import java.nio.ByteBuffer;
       
    36 import java.nio.channels.FileChannel;
       
    37 import java.util.Arrays;
       
    38 
       
    39 /**
       
    40  * @author mike
       
    41  *
       
    42  */
       
    43 public class offsetSingleBuffer implements Testlet
       
    44 {
       
    45 
       
    46   /* (non-Javadoc)
       
    47    * @see gnu.testlet.Testlet#test(gnu.testlet.TestHarness)
       
    48    */
       
    49   public void test(TestHarness harness) 
       
    50   {
       
    51     try
       
    52       {
       
    53         byte[] data = "qwertyuiopasdfghjklzxcvbnm".getBytes("UTF-8");
       
    54         ByteBuffer out = ByteBuffer.allocate(50);
       
    55         out.put(data);
       
    56         out.flip();
       
    57         out.position(5);
       
    58         ByteBuffer in = ByteBuffer.allocate(50);
       
    59         
       
    60         String tmpfile = harness.getTempDirectory() + File.separator
       
    61         + "mauve-offset-single-buffer.tmp";
       
    62         File f = new File(tmpfile);
       
    63         
       
    64         FileOutputStream fOut = new FileOutputStream(f);
       
    65         FileChannel fc = fOut.getChannel();
       
    66         int numBytes = fc.write(out);
       
    67         harness.check(numBytes, data.length - 5, "Number of bytes written");
       
    68         fc.close();
       
    69         
       
    70         harness.check(f.length(), data.length - 5, "Resulting File size");
       
    71         
       
    72         in.position(5);
       
    73         FileInputStream fIn = new FileInputStream(f);
       
    74         FileChannel fcIn = fIn.getChannel();
       
    75         int numRead = fcIn.read(in);
       
    76         harness.check(numRead, data.length - 5, "Number of bytes read");
       
    77         harness.check(in.position(), data.length, "Buffer position");
       
    78         in.flip();
       
    79         byte[] oldData = new byte[data.length - 5];
       
    80         System.arraycopy(data, 5, oldData, 0, 21);
       
    81         byte[] newData = new byte[data.length - 5];
       
    82         in.position(5);
       
    83         in.get(newData);
       
    84         harness.check(Arrays.equals(oldData, newData), "File content");
       
    85         
       
    86         fcIn.close();
       
    87         
       
    88         f.delete();
       
    89       }
       
    90     catch (UnsupportedEncodingException e1)
       
    91       {
       
    92         harness.fail("Unsupported Encoding");
       
    93       }
       
    94     catch (SecurityException e)
       
    95       {
       
    96         harness.fail("Unexpected exception: " + e);
       
    97       }
       
    98     catch (FileNotFoundException e)
       
    99       {
       
   100         harness.fail("Unexpected exception: " + e);
       
   101       }
       
   102     catch (IOException e)
       
   103       {
       
   104         harness.fail("Unexpected exception: " + e);
       
   105       }
       
   106     
       
   107     
       
   108   }
       
   109 
       
   110 }