Example usage for org.springframework.core.io ByteArrayResource ByteArrayResource

List of usage examples for org.springframework.core.io ByteArrayResource ByteArrayResource

Introduction

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

Prototype

public ByteArrayResource(byte[] byteArray) 

Source Link

Document

Create a new ByteArrayResource .

Usage

From source file:org.settings4j.helper.spring.ByteArrayXMLApplicationContext.java

/**
 * @param content The Spring configuration XML as byte[].
 *///from   w  w w .j ava 2  s.  c o  m
public ByteArrayXMLApplicationContext(final byte[] content) {
    super();
    this.configResources = new Resource[1];
    this.configResources[0] = new ByteArrayResource(content);
}

From source file:org.cloudfoundry.identity.uaa.config.YamlMapFactoryBeanTests.java

@SuppressWarnings("unchecked")
@Test//from ww w . j  ava2 s  .  c o  m
public void testOverrideAndremoveDefaults() throws Exception {
    factory.setResources(new ByteArrayResource[] { new ByteArrayResource("foo:\n  bar: spam".getBytes()),
            new ByteArrayResource("foo:\n  spam: bar".getBytes()) });
    assertEquals(1, factory.getObject().size());
    assertEquals(2, ((Map<String, Object>) factory.getObject().get("foo")).size());
}

From source file:org.easit.core.controllers.facebook.FacebookPhotosController.java

private Resource getUploadResource(final String filename, CommonsMultipartFile content) {
    Resource a = new ByteArrayResource(content.getBytes()) {
        public String getFilename() throws IllegalStateException {
            return filename;
        };// w  w  w  . java 2 s. c  om
    };
    return a;
}

From source file:org.springextensions.db4o.io.ResourceStorageTest.java

@Test
public void testOpen() {
    String uri = "resource";
    BinConfiguration binConfiguration = new BinConfiguration(uri, false, 0, false);

    byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    Resource resource = new ByteArrayResource(bytes);

    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    when(resourceLoader.getResource(uri)).thenReturn(resource);

    ResourceStorage resourceStorage = new ResourceStorage();
    resourceStorage.setResourceLoader(resourceLoader);

    Bin bin = resourceStorage.open(binConfiguration);

    Assert.assertEquals(bin.length(), bytes.length);
}

From source file:nl.surfnet.coin.mock.MockSoapServerTest.java

/**
 * Test the MockHttpServer./*from w  ww .  ja  va  2s.c om*/
 * 
 * @throws Exception
 */
@Test
public void testMockHappyFlowWithMultipleResources() throws Exception {
    Resource resource1 = new ByteArrayResource("value".getBytes());
    Resource resource2 = new ByteArrayResource("value2".getBytes());
    super.setResponseResource(new Resource[] { resource1, resource2 });
    HttpResponse response1 = new DefaultHttpClient().execute(new HttpGet("http://localhost:8088/testUrl"));
    HttpResponse response2 = new DefaultHttpClient().execute(new HttpGet("http://localhost:8088/testUrl"));
    String s1 = IOUtils.toString(response1.getEntity().getContent());
    String s2 = IOUtils.toString(response2.getEntity().getContent());
    Assert.assertFalse(s1.equals(s2));
}

From source file:ch.algotrader.hibernate.InMemoryDBTest.java

@After
public void cleanup() throws Exception {

    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ByteArrayResource("DROP ALL OBJECTS".getBytes(Charsets.US_ASCII)));

    DatabasePopulatorUtils.execute(dbPopulator, EmbeddedTestDB.DATABASE.getDataSource());

    if (this.sessionFactory != null) {

        TransactionSynchronizationManager.unbindResource(EmbeddedTestDB.DATABASE.getSessionFactory());
    }// w  w  w  .j a v  a  2s. c o  m
    if (this.session != null) {

        if (this.session.isOpen()) {
            this.session.close();
        }
    }
}

From source file:org.cloudfoundry.identity.uaa.config.YamlPropertiesFactoryBeanTests.java

@Test
@Ignore // We can't fail on duplicate keys because the Map is created by the YAML library
public void testLoadResourcesWithInternalOverride() throws Exception {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(/*from   w  w w  .j av a  2s  . c  o m*/
            new Resource[] { new ByteArrayResource("foo: bar\nspam:\n  foo: baz\nfoo: bucket".getBytes()) });
    Properties properties = factory.getObject();
    assertEquals("bar", properties.get("foo"));
}

From source file:com.ms.commons.test.integration.apachexmlparse.internal.LocalResourceLoader.java

public Resource getResource(String location) {

    System.out.println("Get resource: " + location);
    try {//  w w w. j a v  a2s.  c  o  m
        boolean isFromCache = true;
        String dtdFileName = IntlTestGlobalConstants.TESTCASE_DTD_DIR + File.separator
                + StringUtil.replaceNoWordChars(location) + ".dtd";
        if (DTD_MAP.get(dtdFileName) == null) {
            File dtdFile = new File(dtdFileName);
            if (!dtdFile.exists()) {
                // load dtd from net
                isFromCache = false;
                String dtdContent = StringUtils.join(
                        IOUtils.readLines((new URL(location)).openStream(), "UTF-8"),
                        IntlTestGlobalConstants.LINE_SEPARATOR);
                FileUtils.writeStringToFile(dtdFile, dtdContent, "UTF-8");

                DTD_MAP.put(dtdFileName, dtdContent);
            } else {
                DTD_MAP.put(dtdFileName, FileUtils.readFileToString(dtdFile, "UTF-8"));
            }
        }

        if (isFromCache) {
            System.out.println("Get resource from cache: " + location);
        } else {
            System.out.println("Get resource from net: " + location);
        }

        return new ByteArrayResource(DTD_MAP.get(dtdFileName).getBytes("UTF-8"));
    } catch (Exception e) {
        return parent.getResource(location);
    }
}

From source file:org.alloy.metal.xml.merge.MergeXmlConfigResource.java

public Resource getMergedConfigResource(List<ResourceInputStream> sources) throws BeansException {
    Resource configResource = null;
    ResourceInputStream merged = null;//from w w  w .j a v  a 2s  . co m
    try {
        merged = merge(sources);

        // read the final stream into a byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        boolean eof = false;
        while (!eof) {
            int temp = merged.read();
            if (temp == -1) {
                eof = true;
            } else {
                baos.write(temp);
            }
        }
        configResource = new ByteArrayResource(baos.toByteArray());

        if (LOG.isDebugEnabled()) {
            LOG.debug("Merged config: \n" + serialize(configResource));
        }
    } catch (MergeException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (MergeManagerSetupException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (IOException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } finally {
        if (merged != null) {
            try {
                merged.close();
            } catch (Throwable e) {
                LOG.error("Unable to merge source and patch locations", e);
            }
        }
    }

    return configResource;
}

From source file:jails.http.converter.ResourceHttpMessageConverter.java

public Resource read(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    byte[] body = FileCopyUtils.copyToByteArray(inputMessage.getBody());
    return new ByteArrayResource(body);
}