MappedReadBuffer.java revision 2779:56d1e05e0def
1/*
2 * Copyright (c) 1997, 2017, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26
27/*
28 * The Original Code is HAT. The Initial Developer of the
29 * Original Code is Bill Foote, with contributions from others
30 * at JavaSoft/Sun.
31 */
32
33package jdk.test.lib.hprof.parser;
34
35import java.io.IOException;
36import java.io.RandomAccessFile;
37import java.nio.MappedByteBuffer;
38import java.nio.channels.FileChannel;
39
40/**
41 * Implementation of ReadBuffer using mapped file buffer
42 *
43 * @author A. Sundararajan
44 */
45class MappedReadBuffer implements ReadBuffer {
46    private MappedByteBuffer buf;
47    private RandomAccessFile file;
48
49    MappedReadBuffer(RandomAccessFile file, MappedByteBuffer buf) {
50        this.file = file;
51        this.buf = buf;
52    }
53
54    /**
55     * Factory method to create correct ReadBuffer for a given file.
56     *
57     * The initial purpose of this method was to choose how to read hprof file for parsing
58     * depending on the size of the file and the system property 'jhat.disableFileMap':
59     * "If file size is more than 2 GB and when file mapping is configured (default),
60     * use mapped file reader".
61     *
62     * However, it has been discovered a problem with this approach.
63     * Creating java.nio.MappedByteBuffer from inside the test leads to hprof file
64     * is locked on Windows until test process dies since there is no good way to
65     * release this resource.
66     *
67     * java.nio.MappedByteBuffer will be used only if 'jhat.enableFileMap' is set to true.
68     * Per default 'jhat.enableFileMap' is not set.
69     */
70    static ReadBuffer create(RandomAccessFile file) throws IOException {
71        if (canUseFileMap()) {
72            MappedByteBuffer buf;
73            try {
74                FileChannel ch = file.getChannel();
75                long size = ch.size();
76                buf = ch.map(FileChannel.MapMode.READ_ONLY, 0, size);
77                ch.close();
78                return new MappedReadBuffer(file, buf);
79            } catch (IOException exp) {
80                exp.printStackTrace();
81                System.err.println("File mapping failed, will use direct read");
82                // fall through
83            }
84        } // else fall through
85        return new FileReadBuffer(file);
86    }
87
88    /**
89     * Set system property 'jhat.enableFileMap' to 'true' to enable file mapping.
90     */
91    private static boolean canUseFileMap() {
92        String prop = System.getProperty("jhat.enableFileMap");
93        return prop != null && prop.equals("true");
94    }
95
96    private void seek(long pos) throws IOException {
97        assert pos <= Integer.MAX_VALUE :  "position overflow";
98        buf.position((int)pos);
99    }
100
101    @Override
102    public synchronized char getChar(long pos) throws IOException {
103        seek(pos);
104        return buf.getChar();
105    }
106
107    @Override
108    public synchronized byte getByte(long pos) throws IOException {
109        seek(pos);
110        return buf.get();
111    }
112
113    @Override
114    public synchronized short getShort(long pos) throws IOException {
115        seek(pos);
116        return buf.getShort();
117    }
118
119    @Override
120    public synchronized int getInt(long pos) throws IOException {
121        seek(pos);
122        return buf.getInt();
123    }
124
125    @Override
126    public synchronized long getLong(long pos) throws IOException {
127        seek(pos);
128        return buf.getLong();
129    }
130
131    @Override
132    public void close() throws Exception {
133        file.close();
134    }
135
136}
137