langtools/test/tools/javac/versions/CheckClassFileVersion.java
changeset 25448 40c5263b551f
parent 25447 90222e25bb57
child 25449 3c14a2e16bd6
equal deleted inserted replaced
25447:90222e25bb57 25448:40c5263b551f
     1 /*
       
     2  * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.*;
       
    25 import java.nio.*;
       
    26 import java.nio.channels.*;
       
    27 
       
    28 
       
    29 public class CheckClassFileVersion {
       
    30 
       
    31     static String get(String fn) throws IOException {
       
    32         ByteBuffer bb = ByteBuffer.allocate(1024);
       
    33         FileChannel fc = new FileInputStream(fn).getChannel();
       
    34         try {
       
    35             bb.clear();
       
    36             if (fc.read(bb) < 0)
       
    37                 throw new IOException("Could not read any bytes");
       
    38             bb.flip();
       
    39             int minor = bb.getShort(4);
       
    40             int major = bb.getShort(6);
       
    41             return major + "." + minor;
       
    42         } finally {
       
    43             fc.close();
       
    44         }
       
    45     }
       
    46 
       
    47     public static void main(String[] args) throws IOException {
       
    48         String cfv = get(args[0]);
       
    49         if (args.length == 1) {
       
    50             System.out.println(cfv);
       
    51             return;
       
    52         }
       
    53         if (!cfv.equals(args[1])) {
       
    54             System.err.println("Class-file version mismatch: Expected "
       
    55                                + args[1] + ", got " + cfv);
       
    56             System.exit(1);
       
    57         }
       
    58     }
       
    59 
       
    60 }