ByteResource.java

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 1 1. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → RUN_ERROR
		return new ByteArrayInputStream(bytes);
64
	}
65
66
	@Override
67
	public String getName() {
68 1 1. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → RUN_ERROR
		return name;
69
	}
70
71
	public byte[] getBytes() {
72 1 1. getBytes : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → RUN_ERROR
		return bytes;
73
	}
74
75
	@Override
76
	public int hashCode() {
77 1 1. hashCode : replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → RUN_ERROR
		return new HashCodeBuilder().append(name, bytes).hashCode();
78
	}
79
80
	@Override
81
	public boolean equals(Object obj) {
82 2 1. equals : replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → RUN_ERROR
2. equals : replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → RUN_ERROR
		return new EqualsBuilder(this, obj).appendFields("name", "bytes").isEqual();
83
	}
84
}

Mutations

63

1.1
Location : getInputStream
Killed by :
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → RUN_ERROR

68

1.1
Location : getName
Killed by :
replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → RUN_ERROR

72

1.1
Location : getBytes
Killed by :
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → RUN_ERROR

77

1.1
Location : hashCode
Killed by :
replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → RUN_ERROR

82

1.1
Location : equals
Killed by :
replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → RUN_ERROR

2.2
Location : equals
Killed by :
replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1