Skip to content

Package: CPIOArchiveComponentVisitor

CPIOArchiveComponentVisitor

nameinstructionbranchcomplexitylinemethod
CPIOArchiveComponentVisitor(ArchiveOutputStream)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addEntryToOutoutStream(ArchiveEntry)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
finish()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handleArchiveDevNode(ArchiveDevNode)
M: 6 C: 31
84%
M: 1 C: 2
67%
M: 1 C: 2
67%
M: 1 C: 10
91%
M: 0 C: 1
100%
handleArchiveDirectory(ArchiveDirectory)
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
handleArchiveFile(ArchiveFile)
M: 0 C: 35
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
handleArchiveSymlink(ArchiveSymlink)
M: 0 C: 38
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: package archive;
2:
3: import java.io.ByteArrayInputStream;
4: import java.io.FileNotFoundException;
5: import java.io.IOException;
6: import java.io.InputStream;
7: import java.nio.charset.Charset;
8:
9: import org.apache.commons.compress.archivers.ArchiveEntry;
10: import org.apache.commons.compress.archivers.ArchiveOutputStream;
11: import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
12: import org.apache.commons.compress.utils.IOUtils;
13:
14: /**
15: * Implementation of the ArchiveComponentVisitor to create entries in CPIO archives.
16: *
17: * @author Alex
18: *
19: */
20: public class CPIOArchiveComponentVisitor implements ArchiveComponentVisitor {
21:
22:         /**
23:          * the outputstream to write the archive.
24:          */
25:         private final ArchiveOutputStream outputStream;
26:
27:         /**
28:          *
29:          * @param outputStream
30:          * the outputstream
31:          */
32:         public CPIOArchiveComponentVisitor(final ArchiveOutputStream outputStream) {
33:                 super();
34:                 this.outputStream = outputStream;
35:         }
36:
37:         /**
38:          * adds and closes an entry to the archive.
39:          *
40:          * @param entry
41:          * the entry which should be added
42:          * @throws IOException
43:          * if entry can not be added this.outputstream
44:          */
45:         private void addEntryToOutoutStream(final ArchiveEntry entry) throws IOException {
46:                 outputStream.putArchiveEntry(entry);
47:                 outputStream.closeArchiveEntry();
48:         }
49:
50:         @Override
51:         public void handleArchiveFile(final ArchiveFile archiveFile)
52:                         throws FileNotFoundException, IOException {
53:                 final CpioArchiveEntry entry = new CpioArchiveEntry(archiveFile.getName());
54:                 int value = CpioArchiveEntry.C_ISREG | archiveFile.getMode();
55:                 entry.setMode(value);
56:                 entry.setSize(archiveFile.getArchiveData().getSize());
57:                 outputStream.putArchiveEntry(entry);
58:                 IOUtils.copy(archiveFile.getArchiveData().createInputStream(), outputStream);
59:                 outputStream.closeArchiveEntry();
60:         }
61:
62:         @Override
63:         public void handleArchiveDirectory(final ArchiveDirectory directory)
64:                         throws FileNotFoundException, IOException {
65:                 final CpioArchiveEntry entry = new CpioArchiveEntry(directory.getName());
66:                 int value = CpioArchiveEntry.C_ISDIR | directory.getMode();
67:                 entry.setMode(value);
68:                 entry.setMode(directory.getMode());
69:                 addEntryToOutoutStream(entry);
70:         }
71:
72:         @Override
73:         public void handleArchiveSymlink(final ArchiveSymlink symlink) throws IOException {
74:                 final CpioArchiveEntry entry = new CpioArchiveEntry(symlink.getName());
75:                 byte[] bytes = symlink.getLinktarget().getBytes(Charset.forName("UTF-8"));
76:                 entry.setMode(CpioArchiveEntry.C_ISLNK);
77:                 entry.setSize(bytes.length);
78:                 outputStream.putArchiveEntry(entry);
79:                 InputStream is = new ByteArrayInputStream(bytes);
80:                 IOUtils.copy(is, outputStream);
81:                 outputStream.closeArchiveEntry();
82:         }
83:
84:         @Override
85:         public void handleArchiveDevNode(final ArchiveDevNode node)
86:                         throws IOException, IllegalDeviceTypeException {
87:                 final CpioArchiveEntry entry = new CpioArchiveEntry(node.getName());
88:•                switch (node.getDevtype()) {
89:                 case 'b':
90:                         entry.setMode(CpioArchiveEntry.C_ISBLK);
91:                         break;
92:                 case 'c':
93:                         entry.setMode(CpioArchiveEntry.C_ISCHR);
94:                         break;
95:                 default:
96:                         throw new IllegalDeviceTypeException(node.getDevtype());
97:                 }
98:                 entry.setDeviceMaj(node.getMajor());
99:                 entry.setDeviceMin(node.getMinor());
100:                 addEntryToOutoutStream(entry);
101:         }
102:
103:         @Override
104:         public void finish() throws IOException {
105:                 this.outputStream.finish();
106:         }
107:
108: }