| 1 | package fr.sii.ogham.core.resource; | |
| 2 | ||
| 3 | import java.io.ByteArrayInputStream; | |
| 4 | import java.io.IOException; | |
| 5 | import java.io.InputStream; | |
| 6 | import java.util.Arrays; | |
| 7 | ||
| 8 | import fr.sii.ogham.core.util.EqualsBuilder; | |
| 9 | import fr.sii.ogham.core.util.HashCodeBuilder; | |
| 10 | import fr.sii.ogham.core.util.IOUtils; | |
| 11 | ||
| 12 | /** | |
| 13 | * Basic implementation of a {@link NamedResource} that simply stores a | |
| 14 | * reference to the provided bytes. | |
| 15 | * | |
| 16 | * @author Aurélien Baudet | |
| 17 | * | |
| 18 | */ | |
| 19 | public class ByteResource implements NamedResource { | |
| 20 | /** | |
| 21 | * The content of the resource as array of bytes | |
| 22 | */ | |
| 23 | private byte[] bytes; | |
| 24 | ||
| 25 | /** | |
| 26 | * The name of the resource | |
| 27 | */ | |
| 28 | private String name; | |
| 29 | ||
| 30 | /** | |
| 31 | * Initialize the resource with the provided name and bytes that are | |
| 32 | * immediately read from the stream. The bytes are copied into a new array | |
| 33 | * to prevent security leaks. | |
| 34 | * | |
| 35 | * @param name | |
| 36 | * the name of the resource | |
| 37 | * @param stream | |
| 38 | * the stream that is read | |
| 39 | * @throws IOException | |
| 40 | * when the input stream can't be read | |
| 41 | */ | |
| 42 | public ByteResource(String name, InputStream stream) throws IOException { | |
| 43 | this(name, IOUtils.toByteArray(stream)); | |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Initialize the resource with the provided name and bytes. The bytes are | |
| 48 | * copied into a new array to prevent security leaks. | |
| 49 | * | |
| 50 | * @param name | |
| 51 | * the name of the resource | |
| 52 | * @param bytes | |
| 53 | * the bytes of the resource | |
| 54 | */ | |
| 55 | public ByteResource(String name, byte[] bytes) { | |
| 56 | super(); | |
| 57 | this.name = name; | |
| 58 | this.bytes = Arrays.copyOf(bytes, bytes.length); | |
| 59 | } | |
| 60 | ||
| 61 | @Override | |
| 62 | public InputStream getInputStream() { | |
| 63 |
7
1. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → NO_COVERAGE 2. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → TIMED_OUT 3. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED 4. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED 5. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED 6. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED 7. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED |
return new ByteArrayInputStream(bytes); |
| 64 | } | |
| 65 | ||
| 66 | @Override | |
| 67 | public String getName() { | |
| 68 |
4
1. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → NO_COVERAGE 2. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → SURVIVED 3. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → KILLED 4. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → KILLED |
return name; |
| 69 | } | |
| 70 | ||
| 71 | public byte[] getBytes() { | |
| 72 |
3
1. getBytes : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → NO_COVERAGE 2. getBytes : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → SURVIVED 3. getBytes : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → KILLED |
return bytes; |
| 73 | } | |
| 74 | ||
| 75 | @Override | |
| 76 | public int hashCode() { | |
| 77 |
3
1. hashCode : replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → NO_COVERAGE 2. hashCode : replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → SURVIVED 3. hashCode : replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → KILLED |
return new HashCodeBuilder().append(name, bytes).hashCode(); |
| 78 | } | |
| 79 | ||
| 80 | @Override | |
| 81 | public boolean equals(Object obj) { | |
| 82 |
6
1. equals : replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → NO_COVERAGE 2. equals : replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → NO_COVERAGE 3. equals : replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → SURVIVED 4. equals : replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → KILLED 5. equals : replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → KILLED 6. equals : replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → KILLED |
return new EqualsBuilder(this, obj).appendFields("name", "bytes").isEqual(); |
| 83 | } | |
| 84 | } | |
Mutations | ||
| 63 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
| 68 |
1.1 2.2 3.3 4.4 |
|
| 72 |
1.1 2.2 3.3 |
|
| 77 |
1.1 2.2 3.3 |
|
| 82 |
1.1 2.2 3.3 4.4 5.5 6.6 |