001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * The contents of this file are subject to the Sun Public License
028: * Version 1.0 (the "License"); you may not use this file except in
029: * compliance with the License. A copy of the License is available at
030: * http://www.sun.com/, and in the file LICENSE.html in the
031: * doc directory.
032: *
033: * The Original Code is HAT. The Initial Developer of the
034: * Original Code is Bill Foote, with contributions from others
035: * at JavaSoft/Sun. Portions created by Bill Foote and others
036: * at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved.
037: *
038: * In addition to the formal license, I ask that you don't
039: * change the history or donations files without permission.
040: *
041: */
042:
043: package com.sun.tools.hat.internal.parser;
044:
045: import java.io.IOException;
046: import java.io.RandomAccessFile;
047: import java.nio.MappedByteBuffer;
048: import java.nio.channels.FileChannel;
049:
050: /**
051: * Implementation of ReadBuffer using mapped file buffer
052: *
053: * @(#)MappedReadBuffer.java 1.6 07/05/05
054: * @author A. Sundararajan
055: */
056: class MappedReadBuffer implements ReadBuffer {
057: private MappedByteBuffer buf;
058:
059: MappedReadBuffer(MappedByteBuffer buf) {
060: this .buf = buf;
061: }
062:
063: // factory method to create correct ReadBuffer for a given file
064: static ReadBuffer create(RandomAccessFile file) throws IOException {
065: FileChannel ch = file.getChannel();
066: long size = ch.size();
067: // if file size is more than 2 GB and when file mapping is
068: // configured (default), use mapped file reader
069: if (canUseFileMap() && (size <= Integer.MAX_VALUE)) {
070: MappedByteBuffer buf;
071: try {
072: buf = ch.map(FileChannel.MapMode.READ_ONLY, 0, size);
073: ch.close();
074: return new MappedReadBuffer(buf);
075: } catch (IOException exp) {
076: exp.printStackTrace();
077: System.err
078: .println("File mapping failed, will use direct read");
079: // fall through
080: }
081: } // else fall through
082: return new FileReadBuffer(file);
083: }
084:
085: private static boolean canUseFileMap() {
086: // set jhat.disableFileMap to any value other than "false"
087: // to disable file mapping
088: String prop = System.getProperty("jhat.disableFileMap");
089: return prop == null || prop.equals("false");
090: }
091:
092: private void seek(long pos) throws IOException {
093: assert pos <= Integer.MAX_VALUE : "position overflow";
094: buf.position((int) pos);
095: }
096:
097: public synchronized void get(long pos, byte[] res)
098: throws IOException {
099: seek(pos);
100: buf.get(res);
101: }
102:
103: public synchronized char getChar(long pos) throws IOException {
104: seek(pos);
105: return buf.getChar();
106: }
107:
108: public synchronized byte getByte(long pos) throws IOException {
109: seek(pos);
110: return buf.get();
111: }
112:
113: public synchronized short getShort(long pos) throws IOException {
114: seek(pos);
115: return buf.getShort();
116: }
117:
118: public synchronized int getInt(long pos) throws IOException {
119: seek(pos);
120: return buf.getInt();
121: }
122:
123: public synchronized long getLong(long pos) throws IOException {
124: seek(pos);
125: return buf.getLong();
126: }
127: }
|