alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.parser;

import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.openide.filesystems.FileObject;

/**
 *
 * @author  sdedic
 * @version 
 */
public class Util {
    static final String ATTR_FILE_ENCODING = "Content-Encoding"; // NOI18N
    
    /** Retrieves file's encoding. Returns the default encoding for java files,
     * if the file's specific encoding is not set.
     * Disclaimer: this method is not an API method. It may not
     * be available in future releases.
     * @param someFile file object to retrieve encoding for
     * @return encoding string, or null if no explicit encoding is given in file's
     * or IDE's settings.
     *
     */
    public static String getFileEncoding(FileObject someFile) {
        String enc = (String) someFile.getAttribute(ATTR_FILE_ENCODING);

        if (enc == null) {
// [TODO]            enc = JavaSettings.getDefault().getDefaultEncoding();
        }
        if ("".equals(enc))
            return null;
        else
            return enc;
    }
    
    public static char[] readContents(Reader r) throws IOException {
        int read = 0;
        int total = 0;
        int offset;
        char[] buffer;
        List buflist = new LinkedList();
        
        do {
            buffer = new char[2048];
            offset = 0;
            while (offset < buffer.length) {
                read = r.read(buffer, offset, buffer.length - offset);
                if (read == -1) break;
                offset += read;
            }
            if (offset > 0) buflist.add(buffer);
            total += offset;
        } while (read >= 0);
        r.close();
        
        buffer = new char[total];
        Iterator it = buflist.iterator();
        int offset2 = 0;
        while (it.hasNext()) {
            char[] buf = (char[])it.next();
            int size = (it.hasNext()) ? buf.length : total - offset2;
            System.arraycopy(buf, 0, buffer, offset2, size);
            offset2 += size;
        }
        return buffer;
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.