Read a external file or a resource.

@param filename file/resource to access
@param context printable non-natural language context for errors
@param mh handler for error messages
@return file as startup entry, or null when error (message has been printed)
502457
private static StartupEntry readFile(String filename, String context, MessageHandler mh) {
        if (filename != null) {
            try {
                byte[] encoded = Files.readAllBytes(toPathResolvingUserHome(filename));
                return new StartupEntry(false, filename, new String(encoded),
                        LocalDateTime.now().format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
            } catch (AccessDeniedException e) {
                mh.errormsg("jshell.err.file.not.accessible", context, filename, e.getMessage());
            } catch (NoSuchFileException e) {
                String resource = getResource(filename);
                if (resource != null) {
                    // Not found as file, but found as resource
                    return new StartupEntry(true, filename, resource);
                }
                mh.errormsg("jshell.err.file.not.found", context, filename);
            } catch (Exception e) {
                mh.errormsg("jshell.err.file.exception", context, filename, e);
            }
        } else {
            mh.errormsg("jshell.err.file.filename", context);
        }
        return null;

    }
212094
public static void loadViLocationList(String filename) {
		try {
			FileInputStream in = new FileInputStream(filename);
			if (hsViLocations == null) {
				hsViLocations = new HashSet();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(in, "UTF-8"));
				String line;
				while ((line = reader.readLine()) != null) {
					String word = line.trim();				
					hsViLocations.add(word);
				}
			}
		} catch (Exception e) {
			System.err.print(e.getMessage());
		}
	}
682595
@Override
	public InputStream createResourceAsStream(GeneratorContext context) {

		InputStream is = null;
		if (FileNameUtils.hasImageExtension(context.getPath())) {
			is = helper.createStreamResource(context);
		}

		return is;
	}
1136039
function read(context, file, fileSet, next) {
  var filePath = file.path

  if (file.contents || file.data.unifiedEngineStreamIn) {
    debug('Not reading file `%s` with contents', filePath)
    next()
  } else if (stats(file).fatal) {
    debug('Not reading failed file `%s`', filePath)
    next()
  } else {
    filePath = resolve(context.cwd, filePath)

    debug('Reading `%s` in `%s`', filePath, 'utf8')
    readFile(filePath, 'utf8', onread)
  }

  function onread(error, contents) {
    debug('Read `%s` (error: %s)', filePath, error)

    file.contents = contents || ''

    next(error)
  }
}
308539
static String readResource(String name) throws IOException {
        // Attempt to find the file as a resource
        String spec = String.format(BUILTIN_FILE_PATH_FORMAT, name);

        try (InputStream in = JShellTool.class.getResourceAsStream(spec);
                BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
            return reader.lines().collect(Collectors.joining("\n", "", "\n"));
        }
    }
472061
public InputStream getResourceAsStream(String resourceName, BeanContextChild child) throws IllegalArgumentException
	{
		if (resourceName == null || child == null)
		{
			throw new NullPointerException();
		}
		if (!contains(child))
		{
			throw new IllegalArgumentException(Messages.getString("beans.6D"));
		}

		return ClassLoader.getSystemResourceAsStream(resourceName);
	}
105534
private InputStream startExternal(String fileName) {
		InputStream result = null;
		try {
			String extension = getExtension(fileName);
			String command = externalSupport.get(extension).replace(
					FILEPLACEHOLDER, fileName);
			Process externalProcess = Runtime.getRuntime().exec(command);
			result = externalProcess.getInputStream();
		} catch (IOException ignore) {
		}
		return result;
	}
949540
@Override
    public MIMETypedStream getExternalContent(ContentManagerParams params)
            throws GeneralException, HttpServiceNotFoundException, RangeNotSatisfiableException {
        logger.debug("in getExternalContent(), url={}", params.getUrl());
        try {
            if(params.getProtocol().equals("file")){
                return getFromFilesystem(params);
            }
            if (params.getProtocol().equals("http") || params.getProtocol().equals("https")){
                return getFromWeb(params);
            }
            throw new GeneralException("protocol for retrieval of external content not supported. URL: " + params.getUrl());
        }
        catch (RangeNotSatisfiableException re) {
        	throw re;
        } catch (Exception ex) {
            // catch anything but generalexception
            ex.printStackTrace();
            throw new HttpServiceNotFoundException("[" + this.getClass().getSimpleName() + "] "
                    + "returned an error.  The underlying error was a "
                    + ex.getClass().getName()
                    + "  The message "
                    + "was  \""
                    + ex.getMessage() + "\"  .  ",ex);
        }
    }
618454
protected String handleContextHeader(BufferedReader document)
            throws IOException {
        return (readHeader) ? document.readLine().trim() : null;
    }
1036019
static Startup defaultStartup(MessageHandler mh) {
        if (defaultStartup != null) {
            return defaultStartup;
        }
        try {
            String content = readResource(DEFAULT_STARTUP_NAME);
            return defaultStartup = new Startup(
                    new StartupEntry(true, DEFAULT_STARTUP_NAME, content));
        } catch (AccessDeniedException e) {
            mh.errormsg("jshell.err.file.not.accessible", "jshell", DEFAULT_STARTUP_NAME, e.getMessage());
        } catch (NoSuchFileException e) {
            mh.errormsg("jshell.err.file.not.found", "jshell", DEFAULT_STARTUP_NAME);
        } catch (Exception e) {
            mh.errormsg("jshell.err.file.exception", "jshell", DEFAULT_STARTUP_NAME, e);
        }
        return defaultStartup = noStartup();
    }
237772
public StringBuffer readFile(String filename) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filename));
		StringBuffer xml = new StringBuffer();
		String line = null;
        while((line = reader.readLine()) != null){
            xml.append(line);
        }
		reader.close();
		return xml;
	}
20037
public File asFile(String fileName) throws HelloSignException {
        Integer lastResponseCode = getLastResponseCode();
        File f = null;
        try {
            if (lastResponseCode != null && lastResponseCode != HttpURLConnection.HTTP_OK) {
                this.asJson(); // Will validate response
            }
            f = createTemporaryFile(fileName);
            if (getLastResponseAsFile(f) == 0) {
                logger.warn("No bytes written to file: " + fileName);
            }
        } catch (HelloSignException ex) {
            throw ex;
        } finally {
            reset();
        }
        return f;
    }
386757
public InputStreamReader getFileStream(String filename)
			throws NoSuchPathException{
		try {
			return new InputStreamReader(new FileInputStream(filename));
		} catch (FileNotFoundException e) {
			throw new NoSuchPathException(e);
		}
	}
91991
protected URL resolveHandlerChainFileName(String clzName, String fileName) {
        URL handlerFile = null;
        InputStream in = null;

        String handlerChainFileName = fileName;
        URL baseUrl = classLoader.getResource(getClassResourceName(clzName));

        try {
            //if the filename start with '/', then find and return the resource under the web application home directory directory.
            if (handlerChainFileName.charAt(0) == '/') {
                return classLoader.getResource(handlerChainFileName.substring(1));
            }

            //otherwise, create a new url instance according to the baseurl and the fileName
            handlerFile = new URL(baseUrl, handlerChainFileName);
            in = handlerFile.openStream();
        } catch (Exception e) {
            // log the error msg
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
        }
        return handlerFile;
    }
885214
function read (filename) {
    var data = null
    try {
      data = readfile(filename)
    } catch (error) {
      if (error.code === 'MODULE_NOT_FOUND') {
        console.error('File ' + filename + ' is not found')
      }
    }
    return data
  }
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();
    }
1242631
public Object read(MessageBodyReaderContext context) throws IOException, WebApplicationException {

        Object encoding = context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING);
        if (encoding != null && encoding.toString().equalsIgnoreCase("lzf")) {
            InputStream old = context.getInputStream();
            LZFInputStream is = new LZFInputStream(old);
            context.setInputStream(is);
            try {
                return context.proceed();
            } finally{
                context.setInputStream(old);
            }
        } else {
            return context.proceed();
        }
    }