1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.ctf
.core
;
15 import java
.io
.IOException
;
16 import java
.nio
.ByteBuffer
;
17 import java
.nio
.channels
.FileChannel
;
20 * A common utility for mapping a ByteBuffer safely to work around a bug on
21 * Windows which prevents deleting a file after it was mapped. On Windows, the
22 * ByteBuffer will be allocated and the file will be read instead of being
25 * http://bugs.java.com/view_bug.do?bug_id=4715154
27 public class SafeMappedByteBuffer
{
29 private static final boolean IS_WIN32
= System
.getProperty("os.name").startsWith("Windows"); //$NON-NLS-1$//$NON-NLS-2$
32 * Maps a region of this channel's file directly into memory. On Windows,
33 * this will allocate a new ByteBuffer and read the file.
40 * the position within the file
42 * the size of the region to be mapped (or read)
43 * @return the mapped ByteBuffer
45 * on FileChannel operations failures
47 public static ByteBuffer
map(FileChannel fc
, FileChannel
.MapMode mode
, long position
, long size
) throws IOException
{
48 ByteBuffer byteBuffer
;
50 byteBuffer
= ByteBuffer
.allocate((int) size
);
51 fc
.read(byteBuffer
, position
);
53 byteBuffer
= fc
.map(mode
, position
, size
);
This page took 0.033314 seconds and 5 git commands to generate.