001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.app; 017 018 019import java.io.*; 020 021class Banner { 022 023 public static String getText(String file) { 024 File bannerFile = new File(getRootClassPath(), file); 025 if (bannerFile.exists() && bannerFile.canRead()) { 026 String bannerFileText = readString(bannerFile); 027 if (bannerFileText != null && bannerFileText.trim().length() != 0) { 028 return bannerFileText; 029 } 030 } 031 032 return " ____ ____ ___ ___ ______ \n" + 033 " | || \\ / \\ / \\ | |\n" + 034 " |__ || o )| || || |\n" + 035 " __| || || O || O ||_| |_|\n" + 036 "/ | || O || || | | | \n" + 037 "\\ ` || || || | | | \n" + 038 " \\____||_____| \\___/ \\___/ |__| \n" + 039 " "; 040 041 } 042 043 044 private static String getRootClassPath() { 045 try { 046 String path = getClassLoader().getResource("").toURI().getPath(); 047 return new File(path).getAbsolutePath(); 048 } catch (Exception e) { 049 try { 050 String path = Banner.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 051 path = java.net.URLDecoder.decode(path, "UTF-8"); 052 if (path.endsWith(File.separator)) { 053 path = path.substring(0, path.length() - 1); 054 } 055 /** 056 * Fix path带有文件名 057 */ 058 if (path.endsWith(".jar")) { 059 path = path.substring(0, path.lastIndexOf("/") + 1); 060 } 061 return path; 062 } catch (UnsupportedEncodingException e1) { 063 throw new RuntimeException(e1); 064 } 065 } 066 } 067 068 069 private static ClassLoader getClassLoader() { 070 ClassLoader ret = Thread.currentThread().getContextClassLoader(); 071 return ret != null ? ret : Banner.class.getClassLoader(); 072 } 073 074 075 private static String readString(File file) { 076 ByteArrayOutputStream baos = null; 077 FileInputStream fis = null; 078 try { 079 fis = new FileInputStream(file); 080 baos = new ByteArrayOutputStream(); 081 byte[] buffer = new byte[1024]; 082 for (int len = 0; (len = fis.read(buffer)) > 0; ) { 083 baos.write(buffer, 0, len); 084 } 085 return new String(baos.toByteArray(), "UTF-8"); 086 } catch (Exception e) { 087 } finally { 088 close(fis, baos); 089 } 090 return null; 091 } 092 093 private static void close(InputStream is, OutputStream os) { 094 if (is != null) { 095 try { 096 is.close(); 097 } catch (IOException e) { 098 } 099 } 100 if (os != null) { 101 try { 102 os.close(); 103 } catch (IOException e) { 104 } 105 } 106 } 107 108}