Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 25, 2017
2 parents a0216c2 + edbe85e commit f724e2c
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/main/java/org/cactoos/io/ResourceAsInput.java
@@ -0,0 +1,87 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.io;

import java.io.IOException;
import java.io.InputStream;
import org.cactoos.Input;
import org.cactoos.text.Sprintf;

/**
* Jar class resource.
*
* @author Kirill (g4s8.public@gmail.com)
* @version $Id$
* @see ClassLoader#getResource(String)
* @since 0.1
*/
public final class ResourceAsInput implements Input {

/**
* Resource name.
*/
private final String res;

/**
* Resource class loader.
*/
private final ClassLoader loader;

/**
* New resource input with current context {@link ClassLoader}.
*
* @param res Resource name
*/
public ResourceAsInput(final String res) {
this(
res,
Thread.currentThread().getContextClassLoader()
);
}

/**
* New resource input with specified {@link ClassLoader}.
*
* @param res Resource name
* @param loader Resource class loader
*/
public ResourceAsInput(final String res, final ClassLoader loader) {
this.res = res;
this.loader = loader;
}

@Override
public InputStream open() throws IOException {
final InputStream input = this.loader.getResourceAsStream(this.res);
if (input == null) {
throw new IOException(
new Sprintf(
"Resource '%s' was not found",
this.res
).toString()
);
}
return input;
}
}
66 changes: 66 additions & 0 deletions src/test/java/org/cactoos/io/ResourceAsInputTest.java
@@ -0,0 +1,66 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.io;

import java.util.Arrays;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link ResourceAsInput}.
*
* @author Kirill (g4s8.public@gmail.com)
* @version $Id$
* @since 0.1
*/
public final class ResourceAsInputTest {

/**
* Test built-in resource.
*
* @throws Exception if failed
*/
@Test
public void readResourceTest() throws Exception {
MatcherAssert.assertThat(
Arrays.copyOfRange(
new InputAsBytes(
new ResourceAsInput(
"org/cactoos/io/ResourceAsInputTest.class"
)
).asBytes(),
// @checkstyle MagicNumber (2 lines)
0,
4
),
Matchers.equalTo(
new byte[]{
// @checkstyle MagicNumber (1 line)
(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE,
}
)
);
}
}

0 comments on commit f724e2c

Please sign in to comment.