Convenience method for reading a binary file and base64-encoding it.

@param filename
Filename for reading binary data
@return base64-encoded string or null if unsuccessful
@since 2.1
177474
public static String encodeFromFile(final String filename) {

        java.io.File file = new java.io.File(filename);
        long fileLength = file.length();

        java.io.InputStream ins;
        try {
            ins = new java.io.FileInputStream(file);
        } catch (FileNotFoundException e) {
            System.out.println("Error opening file " + filename + e.getMessage());
            return null;
        }
        String encodedData = encodeFromInputStream(ins, fileLength);
        if (encodedData == null) {
            System.err.println("Error encoding from file " + filename);
        }
        return encodedData;
    }
607638
protected byte[] getFileBytes(String filename) {

        try {
            // is this a zip-file?
            if (m_importZip != null) {
                // yes
                ZipEntry entry = m_importZip.getEntry(filename);

                // path to file might be relative, too
                if ((entry == null) && filename.startsWith("/")) {
                    entry = m_importZip.getEntry(filename.substring(1));
                }
                if (entry == null) {
                    throw new ZipException(
                        Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_FILE_NOT_FOUND_IN_ZIP_1, filename));
                }

                InputStream stream = m_importZip.getInputStream(entry);
                int size = new Long(entry.getSize()).intValue();
                return CmsFileUtil.readFully(stream, size);
            } else {
                // no - use directory
                File file = new File(m_importResource, filename);
                return CmsFileUtil.readFile(file);
            }
        } catch (FileNotFoundException fnfe) {
            if (LOG.isErrorEnabled()) {
                LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_FILE_NOT_FOUND_1, filename), fnfe);
            }
            m_report.println(fnfe);
        } catch (IOException ioe) {
            if (LOG.isErrorEnabled()) {
                LOG.error(
                    Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1, filename),
                    ioe);
            }
            m_report.println(ioe);
        }
        // this will only be returned in case there was an exception
        return "".getBytes();
    }
877155
public static String encodeToString(byte[] src) {
        return src == null ? null : Base64.getEncoder().encodeToString(src);
    }
966576
private static byte[] base64Decode(String value) {
    if (value == null) {
      return null;
    }
    
    try {
      return Base64.decodeBase64(value.getBytes("UTF-8"));
    }
    catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }
524756
public static String readFile(String fileName, String charset) {
        FileInputStream in = null;
        try {
            in = new FileInputStream(fileName);
            return read(in, charset);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeQuietly(in);
        }
    }
238029
def read(filename, binary=True):
    
    with open(filename, 'rb' if binary else 'r') as f:
        return f.read()
693032
private static byte[] getData(String fileName, Object location) throws IOException {

		byte[] data;
		if (location instanceof String) {
			data = FileSupport.getBinaryFromJar(fileName, (String) location);
		} else if (location instanceof File) {
			InputStream in = new FileInputStream((File) location);
			data = StreamSupport.absorbInputStream(in);
			in.close();
		} else {
			throw new NoClassDefFoundError("file '" + fileName + "' could not be loaded from '" + location + '\'');
		}
		return data;
	}
1265772
public String getExtension(String fileName) {
		if (fileName.lastIndexOf('.') > -1) {
			return (fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length())).toLowerCase();
		}
		return null;
	}
824046
public static String getByFilename(String fileName, String defaultType) {
        String extn = getExtension(fileName);
        if (extn != null) {
            return get(extn, defaultType);
        } else {
            // no extension, no content type
            return null;
        }
    }
521329
@Pure
	public static String largeBasename(URL filename) {
		if (filename == null) {
			return null;
		}
		final String fullPath = filename.getPath();
		assert !isWindowsNativeFilename(fullPath);
		int idx;
		int end = fullPath.length();
		do {
			end--;
			idx = fullPath.lastIndexOf(URL_PATH_SEPARATOR_CHAR, end);
		}
		while (idx >= 0 && end >= 0 && idx >= end);
		final String result;
		if (idx < 0) {
			if (end < fullPath.length() - 1) {
				result = fullPath.substring(0, end + 1);
			} else {
				result = fullPath;
			}
		} else {
			result = fullPath.substring(idx + 1, end + 1);
		}
		return decodeHTMLEntities(result);
	}
253228
@Pure
	public static String extension(File filename) {
		if (filename == null) {
			return null;
		}
		final String largeBasename = largeBasename(filename);
		final int idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
		if (idx <= 0) {
			return ""; //$NON-NLS-1$
		}
		return largeBasename.substring(idx);
	}
507773
public static byte[] fromBase64(String base64String) {

        byte[] result = null;
        if (base64String != null) {
            result = Base64.decodeBase64(base64String);
        }
        return result;
    }
311666
public static String getExtension(final String filename) {
        Objects.requireNonNull(filename, "filename cannot be null");
        int lastDotIdx = filename.lastIndexOf(".");
        return lastDotIdx >= 0 ? filename.substring(lastDotIdx) : "";
    }
1337291
public static String getHeaderName(final String basename, final String filename) {
    final String base = basename.replaceAll("\\\\", "/");
    final String file = filename.replaceAll("\\\\", "/");
    if (!file.startsWith(base)) {
      throw new IllegalArgumentException("Error " + file + " does not start with " + base);
    }
    String header = file.substring(base.length() + 1);
    header = header.replaceAll("/", "_");
    header = header.replaceAll("\\.class", ".h");
    return header;
  }
804578
static String base64(byte[] data) {
        ByteBuf encodedData = Unpooled.wrappedBuffer(data);
        ByteBuf encoded = Base64.encode(encodedData);
        String encodedString = encoded.toString(CharsetUtil.UTF_8);
        encoded.release();
        return encodedString;
    }
433074
def read(filename, encoding='utf-8'):
    
    text, encoding = decode( open(filename, 'rb').read() )
    return text, encoding
1333292
public String toBase64() {
        return name
                + (checksum == null ? "null" : Base64.getDecoder().decode(checksum))
                + (lastModified == null ? "null" : lastModified);
    }