<commit_before>/* Copyright (c) 2007 <NAME>, All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.  
 */
package com.sun.jna;

import java.awt.Window;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

/** Provides generation of invocation plumbing for a defined native
 * library interface.
 * <p>
 * {@link #getTypeMapper} and {@link #getStructureAlignment} are provided
 * to avoid having to explicitly pass these parameters to {@link Structure}s, 
 * which would require every {@link Structure} which requries custom mapping
 * or alignment to define a constructor and pass parameters to the superclass.
 * To avoid lots of boilerplate, the base {@link Structure} constructor
 * figures out these properties based on the defining interface.
 * 
 * @see Library
 * @author <NAME>, <EMAIL>
 * @author <EMAIL>
 */
public class Native {

    private static Map typeMappers = Collections.synchronizedMap(new WeakHashMap());
    private static Map alignments = Collections.synchronizedMap(new WeakHashMap());
    
    private Native() { }
    
    /** Utility method to get the native window ID for a Java {@link Window}
     * as a <code>long</code> value.
     * This method is primarily for X11-based systems, which use an opaque
     * <code>XID</code> (usually <code>long int</code>) to identify windows. 
     */
    public static long getWindowID(Window w) {
        if (!w.isDisplayable()) 
            throw new IllegalStateException("Window is not yet displayable");
        // On X11 VMs prior to 1.5, the window must be visible
        if (System.getProperty("java.version").matches("^1\\.4\\..*")) {
            if (!w.isVisible()) {
                throw new IllegalStateException("Window is not yet visible");
            }
        }
        return getWindowHandle0(w);
    }
    
    /** Utility method to get the native window pointer for a Java 
     * {@link Window} as a {@link Pointer} value.  This method is primarily for 
     * Windows, which uses the <code>HANDLE</code> type (actually 
     * <code>void *</code>) to identify windows. 
     */
    public static Pointer getWindowPointer(Window w) {
        if (!w.isDisplayable())
            throw new IllegalStateException("Window is not yet displayable");
        return new Pointer(getWindowHandle0(w));
    }
    
    private static native long getWindowHandle0(Window w);
    
    /** Convert a direct {@link ByteBuffer} into a {@link Pointer}. 
     * @throws IllegalArgumentException if the byte buffer is not direct.
     */
    public static native Pointer getByteBufferPointer(ByteBuffer b);
    
    /** Obtain a Java String from the given native char array. */
    public static String toString(byte[] buf) {
        String s = new String(buf);
        return s.substring(0, s.indexOf(0));
    }
    
    /** Obtain a Java String from the given native wchar_t array. */
    public static String toString(char[] buf) {
        String s = new String(buf); 
        return s.substring(0, s.indexOf(0));
    }
    
    /** Load a library interface from the given shared library, providing
     * the explicit interface class.<commit_msg>Avoid potential errors converting native byte[]/char[] to String<commit_after>/* Copyright (c) 2007 <NAME>, All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.  
 */
package com.sun.jna;

import java.awt.Window;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

/** Provides generation of invocation plumbing for a defined native
 * library interface.
 * <p>
 * {@link #getTypeMapper} and {@link #getStructureAlignment} are provided
 * to avoid having to explicitly pass these parameters to {@link Structure}s, 
 * which would require every {@link Structure} which requries custom mapping
 * or alignment to define a constructor and pass parameters to the superclass.
 * To avoid lots of boilerplate, the base {@link Structure} constructor
 * figures out these properties based on the defining interface.
 * 
 * @see Library
 * @author <NAME>, <EMAIL>
 * @author <EMAIL>
 */
public class Native {

    private static Map typeMappers = Collections.synchronizedMap(new WeakHashMap());
    private static Map alignments = Collections.synchronizedMap(new WeakHashMap());
    
    private Native() { }
    
    /** Utility method to get the native window ID for a Java {@link Window}
     * as a <code>long</code> value.
     * This method is primarily for X11-based systems, which use an opaque
     * <code>XID</code> (usually <code>long int</code>) to identify windows. 
     */
    public static long getWindowID(Window w) {
        if (!w.isDisplayable()) 
            throw new IllegalStateException("Window is not yet displayable");
        // On X11 VMs prior to 1.5, the window must be visible
        if (System.getProperty("java.version").matches("^1\\.4\\..*")) {
            if (!w.isVisible()) {
                throw new IllegalStateException("Window is not yet visible");
            }
        }
        return getWindowHandle0(w);
    }
    
    /** Utility method to get the native window pointer for a Java 
     * {@link Window} as a {@link Pointer} value.  This method is primarily for 
     * Windows, which uses the <code>HANDLE</code> type (actually 
     * <code>void *</code>) to identify windows. 
     */
    public static Pointer getWindowPointer(Window w) {
        if (!w.isDisplayable())
            throw new IllegalStateException("Window is not yet displayable");
        return new Pointer(getWindowHandle0(w));
    }
    
    private static native long getWindowHandle0(Window w);
    
    /** Convert a direct {@link ByteBuffer} into a {@link Pointer}. 
     * @throws IllegalArgumentException if the byte buffer is not direct.
     */
    public static native Pointer getByteBufferPointer(ByteBuffer b);
    
    /** Obtain a Java String from the given native char array.  If there is
     * no NUL terminator, the String will comprise the entire array.
     */
    public static String toString(byte[] buf) {
        String s = new String(buf);
        int term = s.indexOf(0);
        if (term != -1)
            s = s.substring(0, term);
        return s;
    }
    
    /** Obtain a Java String from the given native wchar_t array.  If there is
     * no NUL terminator, the String will comprise the entire array.
     */
    public static String toString(char[] buf) {
        String s = new String(buf); 
        int term = s.indexOf(0);
        if (term != -1)
            s = s.substring(0, term);
        return s;
    }
    
    /** Load a library interface from the given shared library, providing
     * the explicit interface class.
{'fraction_non_alphanumeric': 0.09300287895856803, 'fraction_numerical': 0.005507572912755038, 'mean_word_length': 2.9930034982508746, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3291594', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2073, 'n_words': 884}