Example usage for org.springframework.core.io Resource getInputStream

List of usage examples for org.springframework.core.io Resource getInputStream

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getInputStream.

Prototype

InputStream getInputStream() throws IOException;

Source Link

Document

Return an InputStream for the content of an underlying resource.

Usage

From source file:ru.jts_dev.gameserver.parser.impl.SettingsHolder.java

@PostConstruct
private void parse() throws IOException {
    log.info("Loading data file: setting.txt");
    final Resource file = context.getResource("scripts/setting.txt");
    try (InputStream is = file.getInputStream()) {
        final ANTLRInputStream input = new ANTLRInputStream(is);
        final SettingsLexer lexer = new SettingsLexer(input);
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final SettingsParser parser = new SettingsParser(tokens);

        final ParseTree tree = parser.file();
        final ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(this, tree);
    }//from   w  ww  .  j av a2 s  .  co  m
}

From source file:com.fns.grivet.service.NamedQueryServiceSelectTest.java

@Test
public void testCreateThenGetParamsNotSupplied() throws IOException {
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
        Resource r = resolver.getResource("classpath:TestSelectQuery.json");
        String json = IOUtils.toString(r.getInputStream(), Charset.defaultCharset());
        NamedQuery namedQuery = objectMapper.readValue(json, NamedQuery.class);
        namedQueryService.create(namedQuery);

        namedQueryService.get("getAttributesCreatedBefore", null);
    });//from   w ww. ja  v a 2s  . c  o m
}

From source file:com.qdeve.oilprice.reader.ContentReader.java

private String getContentFromFile() {
    String body = null;/* w  w w. ja v a 2s .com*/
    Resource resource = applCtx.getResource(properties.getContentPath());
    InputStream inputStream;
    try {
        inputStream = resource.getInputStream();
        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, DEFAULT_ENCODING);
        body = writer.toString();
    } catch (IOException e) {
        throw new ProcessingException("Failed to retrieve content from " + properties.getContentPath()
                + ", reason: " + e.getMessage(), e);
    }
    return body;
}

From source file:org.solmix.runtime.support.spring.SpringResourceResolver.java

@Override
public InputStream getAsStream(String name) {
    Resource r = context.getResource(name);
    if (r != null && r.exists()) {
        try {//from w  w  w. java2s. c  o m
            return r.getInputStream();
        } catch (IOException e) {
            //ignore and return null
        }
    }
    r = context.getResource("/" + name);
    if (r != null && r.exists()) {
        try {
            return r.getInputStream();
        } catch (IOException e) {
            //ignore and return null
        }
    }
    return null;
}

From source file:com.fns.grivet.service.NamedQueryServiceSelectTest.java

@Test
public void testCreateThenGetIncorrectParamsSupplied() throws IOException {
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
        Resource r = resolver.getResource("classpath:TestSelectQuery.json");
        String json = IOUtils.toString(r.getInputStream(), Charset.defaultCharset());
        NamedQuery namedQuery = objectMapper.readValue(json, NamedQuery.class);
        namedQueryService.create(namedQuery);

        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("timedCreated", LocalDateTime.now().plusDays(1).toString());
        namedQueryService.get("getAttributesCreatedBefore", params);
    });//ww w .j av  a  2s .c o m
}

From source file:com.acme.legacy.app.repository.JsonUserRepository.java

@PostConstruct
@SuppressWarnings("unchecked")
public void init() throws Exception {
    Resource resource = new ClassPathResource("users.json");
    ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build();
    List<User> userList = mapper.readValue(resource.getInputStream(), new TypeReference<List<User>>() {
    });//  w  w  w. java2 s. c o m
    Map<String, User> userMap = new TreeMap<>();

    for (User user : userList)
        userMap.put(user.getEmail(), user);

    this.users = Collections.unmodifiableMap(userMap);
}

From source file:cn.cdwx.jpa.utils.PropertiesLoader.java

/**
 * , Spring Resource?./*from w  w w. j  av a 2 s  . c o  m*/
 */
private Properties loadProperties(String... resourcesPaths) {
    Properties props = new Properties();

    for (String location : resourcesPaths) {

        logger.debug("Loading properties file from path:{}", location);

        InputStream is = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            is = resource.getInputStream();
            props.load(is);
        } catch (IOException ex) {
            logger.info("Could not load properties from path:{}, {} ", location, ex.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return props;
}

From source file:org.data.support.beans.factory.xml.ResourceEntityResolver.java

@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
    InputSource source = super.resolveEntity(publicId, systemId);
    if (source == null && systemId != null) {
        String resourcePath = null;
        try {// www .jav  a  2  s  .  com
            String decodedSystemId = URLDecoder.decode(systemId);
            String givenUrl = new URL(decodedSystemId).toString();
            String systemRootUrl = new File("").toURL().toString();
            // Try relative to resource base if currently in system root.
            if (givenUrl.startsWith(systemRootUrl)) {
                resourcePath = givenUrl.substring(systemRootUrl.length());
            }
        } catch (Exception ex) {
            // Typically a MalformedURLException or AccessControlException.
            if (logger.isDebugEnabled()) {
                logger.debug("Could not resolve XML entity [" + systemId + "] against system root URL", ex);
            }
            // No URL (or no resolvable URL) -> try relative to resource base.
            resourcePath = systemId;
        }
        if (resourcePath != null) {
            if (logger.isTraceEnabled()) {
                logger.trace(
                        "Trying to locate XML entity [" + systemId + "] as resource [" + resourcePath + "]");
            }
            Resource resource = this.resourceLoader.getResource(resourcePath);
            source = new InputSource(resource.getInputStream());
            source.setPublicId(publicId);
            source.setSystemId(systemId);
            if (logger.isDebugEnabled()) {
                logger.debug("Found XML entity [" + systemId + "]: " + resource);
            }
        }
    }
    return source;
}

From source file:org.paxml.file.LineBasedFile.java

public LineBasedFile(Resource file, String encoding) {
    name = PaxmlUtils.getResourceFile(file);
    InputStream in = null;/*from   ww w  .j  a  va  2  s  . co  m*/
    try {
        in = file.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in, encoding == null ? "UTF-8" : encoding));
        // cache the 1st line
        line = reader.readLine();
    } catch (IOException e) {
        IOUtils.closeQuietly(in);
        throw new PaxmlRuntimeException("Cannot read file: " + name, e);
    }
}

From source file:com.restjplat.core.utils.PropertiesLoader.java

/**
 * , Spring Resource?./* w ww. java2 s .c  om*/
 */
private Properties loadProperties(String... resourcesPaths) {
    Properties props = new Properties();

    for (String location : resourcesPaths) {
        InputStream is = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            is = resource.getInputStream();
            props.load(is);
        } catch (IOException ex) {
            logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return props;
}