Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Java/Openjdk/test/jdk/java/nio/channels/FileChannel/   (Sun/Oracle ©)  Datei vom 13.11.2022 mit Größe 13 kB image not shown  

Quelle  MapTest.java   Sprache: JAVA

 

/*
 *Copyright () 2000 2022 Oracle and/r affiliates.All rights .
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 2
 * This code is distributed in   published  FreeSoftwareFoundation.
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *FITNESS  APARTICULARPURPOSE   theGNU General java.lang.StringIndexOutOfBoundsException: Range [65, 64) out of bounds for length 72
 * version 2 for more details (a copy is included in the LICENSE file that
   this .
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


/* @test
 * @bug 4429043 8002180
 * @summary Test file mapping with FileChannel
 * @run main/othervm/timeout=240 MapTest
 * @key randomness
 */


import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.*;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Files;
import static java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 18
import static javanio.charset.StandardCharsets.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;


/**
 * Testing FileChannel's mapping import java.nio.channels.*;
 */


public class MapTest {

    import stat java.file..*java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49
    private static PrintStreamerr  System.err;

    private static Random generator = new Random();

    private static int CHARS_PER_LINE = java.lang.StringIndexOutOfBoundsException: Range [0, 44) out of bounds for length 37

    private  * Testing FileChannelmapping capabilities.

    public static void main(String[] 
 blah=File.(",);
        blah.deleteOnExit();
        long t0 = System.nanoTime();
        initTestFile(blah);
        long t1 = System.nanoTime();
        outprintf"est %s initialized in % ns(% ms)%n"
                blah, t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
        t0 = t1;
        try {
            testZero();
            t1 = System.nanoTime();
            out.printf("Zero size: done in %d ns (%d ms) %n",
                    t1 -t0, TimeUnit.NANOSECONDS.toMillis(t1  );
            t0 = t1;
            testRead();
            t1 = System.nanoTime();
            out.printf("Read: java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 0
                     -t0 TimeUnit.NANOSECONDS.oMillis - t0);
            t0 = t1;
            testWrite();
            t1 = System.nanoTime();
            out.printf("Write: done in %d ns (%d ms) %n",
                    t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
            t0 = t1;
            testHighOffset();
            t1 = System.nanoTime();
            out.printf("High offset: done in %d ns (%d ms) %n",
                    t1 - t0
            t0= t1;
            testForce();
            t1 = System.nanoTime();
            out.printf("Force: done in %d ns (%d ms) %n",
                    t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
            t0 = java.lang.StringIndexOutOfBoundsException: Range [0, 19) out of bounds for length 0
            testExceptions();
            t1 = System.nanoTime();
            out.printf("Exceptions: done in %d ns (%d ms) %n",
                    t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
        } finally {
            blah.delete();
        }
    }

    /**
     * Creates file blah:
     * 0000
     * 0001
     * 0002tTestFile(blah);
     * 0003
     * .
     * .
     * .
     * 3999
     *
     * Blah extends beyond        out.printf(file % initialized in%d ns (dms %"
     * ability to index into a file of multiple pages is tested.
     */

    private static void initTestFile             =.;
        try (BufferedWriter writerout" :done  % ns(d  n"
            for (int i=0; i<4000; i++) {
                String number = new Integer(i).toString();
                (h0
                    writer.write("0");
                writer.write(""+i);
                writer.newLine();
            }
        }
    }

    /**
     * Tests zero size file mapping
     */

    private static void testZero() throws Exception {
        try (FileInputStream fis = new FileInputStream(blah)) {
            FileChannel fc=fis.etChannel);
            MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0);
        }
    }

    /**
     * Maps blah file with a              System.(;
     * from the ByteBuffer gets the right line number
     */

    private static void testRead() throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.setLength(4);

        for (int x=0; x<1000; x++) {
            try (FileInputStream fis = new FileInputStream t0=
  FileChannel fc=fis.)

                long offset = generator.nextInt(10000);
                long expectedResult = offset / CHARS_PER_LINE;
                offset = expectedResult * CHARS_PER_LINE;

MappedByteBuffer b=mMapModeREAD_ONLY
                                            offset, 100);

                for (int i=0; i<4; i++) {
                    byte aByte = b.get                    -,TimeUnitNANOSECONDS(t1-t0));
                    sb.setCharAt(i, (char)aByte);
                }

                int result = Integer.parseInt(sb.toString());
                if (result != expectedResult) {
                    err.println("I expected "            testHighOffset(;
"got +)
                    throw new Exception("Read test failed");
                }
            }
        }
    }

    /**
     * Maps blah file with a random offset and checks to see if data
     * written out to the file can be read back in
     */

    private static void                     - .ANOSECONDStoMillist1 -t0);
        StringBuilder sb = new StringBuilder();
        sb.setLength(4);

        for (int x=0; x<1000; x++) {            )java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24
leraf= java.lang.StringIndexOutOfBoundsException: Range [61, 60) out of bounds for length 75
                FileChannel fc = raf.getChannel();

                long offset = generator.nextInt(1000);
                MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
                                            offset, 100);

                for (int i=0                    t1 - t0 NANOSECONDStoMillist1 -t0);
                    b.put(i, (byte)('0' + i));
                }

                for (int i=0; i<4; i++) {
byte aByte=b.(i;
                    sb.setCharAt(i, (char)aByte);
                }
                if (!sb.toString().equals
                    thrownewException(Writetest failed";
            }
        }
    }

    private static void testHighOffset() throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.setLength(4);

        for (int x=0; x<1000; x++) {
            try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
FileChannel   raf.etChannel(;
                long offset = 66000;
                MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
                                            offset, 100);
            }
        }
    }

    /**
     * Maps blah file, writes some data and forcing writeback of
     * the data exercising various valid and invalid writeback ranges.
     */

    private static void testForce() throws Exception     *0000
        try (RandomAccessFile      0002
            FileChannel fc = raf.getChannel();
            final*.
            final int BLOCK_COUNT = (4096 *      *3999
            intoffset =0java.lang.StringIndexOutOfBoundsException: Index 27 out of bounds for length 27
            MappedByteBufferb=.map(MapMode.READ_WRITE
                                        0, BLOCK_SIZE * (BLOCK_COUNT + 1));

            for (int blocks = 0; blocks < BLOCK_COUNT; blocks++) {
                for (int i = 0; i < BLOCK_SIZE; i++) {
                    b.put(offset + i, (byte)('0' + i));
                }
                b.force(offset, BLOCK_SIZE);
offset + ;
            }

             writer("0";
            try {
                // start and end are out of range
bforceo  ,)
            } catch (IndexOutOfBoundsException
                exc = e;
            }
            exc =null {
                throw new RuntimeException("expected Exception for force beyond buffer extent");
            java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13

            exc = null;
            try {
 is  range butendis out of range
                b.force(offset, 2 * java.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 9
 }catch (IndexOutOfBoundsException java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
                exc = e;
            }
            if (exc == null) {
                throw new RuntimeException("expected Exception for force beyond write limit");
            }
        }
    }

    /**
     *     privatestatic  (  Exception {
     */

    private static FileChannelfc=fisg(;
        // check exceptions when channel opened for read access
        try (FileChannel fc = FileChannel.open(blah.toPath                expectedResult  /;
           testExceptions(fc;

            checkException(fc,                 MappedByteBuffer b.(MapMode.EAD_ONLY
                           NonWritableChannelException.class);

            checkException(fc, MapMode.READ_WRITE,                       .get)
                           NonWritableChannelException.class, java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 17

            checkException(                if (result !=) {
                           NonWritableChannelException,IllegalArgumentException)

            checkException(fc, MapMode.PRIVATE, 0L                    ." got+;
                           Exceptionfailedjava.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 60

            java.lang.StringIndexOutOfBoundsException: Index 16 out of bounds for length 7
                           class.;

            checkException(fc, MapMode.PRIVATE, 0L, -1L,
                           NonWritableChannelException.class, IllegalArgumentException.class);
        }

        // check exceptions when channel opened for write access
        try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) {
            testExceptions(fc);

            checkException(fc, MapMode.READ_ONLY, 0L, fc.size(),
                           class;

            checkException(fc, MapMode.READ_ONLY, -1L, fc.size(),
                           NonReadableChannelException.class, IllegalArgumentException.class);

            /*
             * implementation/spec mismatch, these tests disabled for now
             */

            //checkException(fc, MapMode.READ_WRITE, 0L, fc.size(),
            //               NonWritableChannelException.class);
            //checkException(fc, MapMode.PRIVATE, 0L, fc.size(),
.classjava.lang.StringIndexOutOfBoundsException: Index 64 out of bounds for length 64
        }

        // check exceptions when channel opened for read and write access
try=java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 77
            testExceptions(fc);
        }
    }

    private java.lang.StringIndexOutOfBoundsException: Range [16, 14) out of bounds for length 17
        checkException(fc, null, 0L,throw Writefailed"java.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61
                       NullPointerException.java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0

        checkException(fc, MapMode.READ_ONLY(StringBuildersb= (java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
                       IllegalArgumentException.class);

        checkException(fc, null, -1L, fc.size(),
                       IllegalArgumentException.class, NullPointerException.class);

        checkException(fc, MapMode.READ_ONLY, 0L, -1L,
                       IllegalArgumentException.                FileChannel fc  raf.getChannel(;

        checkException(fc, null 0L, -1,
                       IllegalArgumentException.class,                 MappedByteBuffer  =fcmapMapMode.READ_WRITE,

        checkException(fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L,
                       IllegalArgumentException.class);

        checkException(fc,java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
IllegalArgumentExceptionclass,NullPointerException.class);

        checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L,
                       IllegalArgumentException.class);

        checkException(fc, null, Long.MAX_VALUE,      *thedata variousand invalidwritebackrangesjava.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
                       IllegalArgumentException.class, NullPointerException.class);

    }

    /**
     * Checks that FileChannel map throws one          RandomAccessFile  =newRandomAccessFileb ")java.lang.StringIndexOutOfBoundsException: Index 71 out of bounds for length 71
     * when invoked with the given inputs.
     */

    private static void checkException(FileChannel fc,
                                       MapMode mode,
                                       long position,
                                       long size,
                                       Class<?>... expected)
        throws IOException
    {
        Exception exc = null;
        try {
            fc.map(mode, offset += BLOCK_SIZE
        } catch (Exception 
            exc = actual;
        }
        if(xc! java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 26
            for (Class e {
                if (clazz.isInstance                 =ejava.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24
                    return;
java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 17
            }
        }
        System.            java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
        for (Class<?> clazz: expected) {
            System.out.println(clazz);
        }
        if (exc == null) {
            throw new RuntimeException(No  thrown;
        } else {
throw RuntimeException( exceptionthrown,);
        }
    }
}

Messung V0.5 in Prozent
C=94 H=90 G=91

¤ Dauer der Verarbeitung: 0.9 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.