Author: bodewig
Date: Tue Jan 7 13:37:45 2014
New Revision: 1556213
URL: http://svn.apache.org/r1556213
Log:
Output seems to relatively straight forward to port. No tests, though.
Added:
commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveOutput.java
- copied, changed from r1556168,
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
Copied:
commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveOutput.java
(from r1556168,
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java)
URL:
http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveOutput.java?p2=commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveOutput.java&p1=commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java&r1=1556168&r2=1556213&rev=1556213&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
(original)
+++
commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/formats/ar/ArArchiveOutput.java
Tue Jan 7 13:37:45 2014
@@ -16,29 +16,29 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.commons.compress.archivers.ar;
+package org.apache.commons.compress2.formats.ar;
-import java.io.File;
import java.io.IOException;
-import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.StandardCharsets;
-import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.archivers.ArchiveOutputStream;
-import org.apache.commons.compress.utils.ArchiveUtils;
+import org.apache.commons.compress2.archivers.ArchiveEntryParameters;
+import org.apache.commons.compress2.archivers.spi.AbstractArchiveOutput;
/**
* Implements the "ar" archive format as an output stream.
*
* @NotThreadSafe
*/
-public class ArArchiveOutputStream extends ArchiveOutputStream {
+public class ArArchiveOutput extends AbstractArchiveOutput<ArArchiveEntry> {
/** Fail if a long file name is required in the archive. */
public static final int LONGFILE_ERROR = 0;
/** BSD ar extensions are used to store long file names in the archive. */
public static final int LONGFILE_BSD = 1;
- private final OutputStream out;
+ private final WritableByteChannel out;
private long entryOffset = 0;
private ArArchiveEntry prevEntry;
private boolean haveUnclosedEntry = false;
@@ -47,7 +47,7 @@ public class ArArchiveOutputStream exten
/** indicates if this archive is finished */
private boolean finished = false;
- public ArArchiveOutputStream( final OutputStream pOut ) {
+ public ArArchiveOutput(WritableByteChannel pOut) {
this.out = pOut;
}
@@ -57,75 +57,80 @@ public class ArArchiveOutputStream exten
* This specifies the treatment of long file names (names >= 16).
* Default is LONGFILE_ERROR.
* @param longFileMode the mode to use
- * @since 1.3
*/
public void setLongFileMode(int longFileMode) {
this.longFileMode = longFileMode;
}
private long writeArchiveHeader() throws IOException {
- byte [] header = ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER);
+ ByteBuffer header =
StandardCharsets.US_ASCII.encode(ArArchiveEntry.HEADER);
+ int len = header.remaining();
out.write(header);
- return header.length;
+ count(len);
+ return len;
}
@Override
- public void closeArchiveEntry() throws IOException {
- if(finished) {
+ public void closeEntry() throws IOException {
+ if (finished) {
throw new IOException("Stream has already been finished");
}
if (prevEntry == null || !haveUnclosedEntry){
throw new IOException("No current entry to close");
}
if (entryOffset % 2 != 0) {
- out.write('\n'); // Pad byte
+ out.write(ByteBuffer.wrap(new byte[] { '\n' })); // Pad byte
+ count(1);
}
haveUnclosedEntry = false;
}
@Override
- public void putArchiveEntry( final ArchiveEntry pEntry ) throws
IOException {
- if(finished) {
+ public void putEntry(final ArArchiveEntry entry) throws IOException {
+ if (finished) {
throw new IOException("Stream has already been finished");
}
- ArArchiveEntry pArEntry = (ArArchiveEntry)pEntry;
if (prevEntry == null) {
writeArchiveHeader();
} else {
- if (prevEntry.getLength() != entryOffset) {
- throw new IOException("length does not match entry (" +
prevEntry.getLength() + " != " + entryOffset);
+ if (prevEntry.getSize() != entryOffset) {
+ throw new IOException("length does not match entry (" +
prevEntry.getSize() + " != " + entryOffset);
}
if (haveUnclosedEntry) {
- closeArchiveEntry();
+ closeEntry();
}
}
- prevEntry = pArEntry;
+ prevEntry = entry;
- writeEntryHeader(pArEntry);
+ writeEntryHeader(entry);
entryOffset = 0;
haveUnclosedEntry = true;
}
- private long fill( final long pOffset, final long pNewOffset, final char
pFill ) throws IOException {
+ private long fill( final long pOffset, final long pNewOffset, byte pFill )
throws IOException {
final long diff = pNewOffset - pOffset;
+ if (diff > Integer.MAX_VALUE) {
+ throw new IOException("filling too much");
+ }
if (diff > 0) {
+ ByteBuffer b = ByteBuffer.allocate((int) diff);
for (int i = 0; i < diff; i++) {
- write(pFill);
+ b.put(pFill);
}
+ b.flip();
+ write(b);
}
return pNewOffset;
}
private long write( final String data ) throws IOException {
- final byte[] bytes = data.getBytes("ascii");
- write(bytes);
- return bytes.length;
+ return write(StandardCharsets.US_ASCII.encode(data));
}
private long writeEntryHeader( final ArArchiveEntry pEntry ) throws
IOException {
@@ -140,50 +145,50 @@ public class ArArchiveOutputStream exten
if (LONGFILE_BSD == longFileMode &&
(n.length() > 16 || n.indexOf(" ") > -1)) {
mustAppendName = true;
- offset += write(ArArchiveInputStream.BSD_LONGNAME_PREFIX
- + String.valueOf(n.length()));
+ // TODO re-introduce constant
+ offset += write("#1/" + String.valueOf(n.length()));
} else {
offset += write(n);
}
- offset = fill(offset, 16, ' ');
- final String m = "" + pEntry.getLastModified();
+ offset = fill(offset, 16, (byte) ' ');
+ final String m = "" + pEntry.getLastModifiedDate();
if (m.length() > 12) {
throw new IOException("modified too long");
}
offset += write(m);
- offset = fill(offset, 28, ' ');
- final String u = "" + pEntry.getUserId();
+ offset = fill(offset, 28, (byte) ' ');
+ final String u = "" + pEntry.getOwnerInformation().getUserId();
if (u.length() > 6) {
throw new IOException("userid too long");
}
offset += write(u);
- offset = fill(offset, 34, ' ');
- final String g = "" + pEntry.getGroupId();
+ offset = fill(offset, 34, (byte) ' ');
+ final String g = "" + pEntry.getOwnerInformation().getGroupId();
if (g.length() > 6) {
throw new IOException("groupid too long");
}
offset += write(g);
- offset = fill(offset, 40, ' ');
+ offset = fill(offset, 40, (byte) ' ');
final String fm = "" + Integer.toString(pEntry.getMode(), 8);
if (fm.length() > 8) {
throw new IOException("filemode too long");
}
offset += write(fm);
- offset = fill(offset, 48, ' ');
+ offset = fill(offset, 48, (byte) ' ');
final String s =
- String.valueOf(pEntry.getLength()
+ String.valueOf(pEntry.getSize()
+ (mustAppendName ? n.length() : 0));
if (s.length() > 10) {
throw new IOException("size too long");
}
offset += write(s);
- offset = fill(offset, 58, ' ');
+ offset = fill(offset, 58, (byte) ' ');
offset += write(ArArchiveEntry.TRAILER);
@@ -195,18 +200,19 @@ public class ArArchiveOutputStream exten
}
@Override
- public void write(byte[] b, int off, int len) throws IOException {
- out.write(b, off, len);
+ public int write(ByteBuffer b) throws IOException {
+ int len = out.write(b);
count(len);
entryOffset += len;
+ return len;
}
/**
- * Calls finish if necessary, and then closes the OutputStream
+ * Calls finish if necessary, and then closes the nested Channel
*/
@Override
public void close() throws IOException {
- if(!finished) {
+ if (!finished) {
finish();
}
out.close();
@@ -214,21 +220,22 @@ public class ArArchiveOutputStream exten
}
@Override
- public ArchiveEntry createArchiveEntry(File inputFile, String entryName)
- throws IOException {
- if(finished) {
- throw new IOException("Stream has already been finished");
- }
- return new ArArchiveEntry(inputFile, entryName);
+ public ArArchiveEntry createEntry(ArchiveEntryParameters params) {
+ return new ArArchiveEntry(params);
}
@Override
public void finish() throws IOException {
- if(haveUnclosedEntry) {
+ if (haveUnclosedEntry) {
throw new IOException("This archive contains unclosed entries.");
} else if(finished) {
throw new IOException("This archive has already been finished");
}
finished = true;
}
+
+ @Override
+ public boolean isOpen() {
+ return out.isOpen();
+ }
}