Refactor DexWriter to write to a generic OutputStream

This commit is contained in:
Ben Gruver 2012-12-28 16:28:35 -08:00
parent 9393c08e36
commit 9a90c5560c
3 changed files with 85 additions and 93 deletions

View File

@ -36,6 +36,7 @@ import org.jf.dexlib2.iface.AnnotationElement;
import org.jf.dexlib2.iface.ClassDef;
import org.jf.dexlib2.iface.value.*;
import org.jf.util.ExceptionWithContext;
import org.jf.util.RandomAccessFileOutputStream;
import javax.annotation.Nonnull;
import java.io.IOException;
@ -232,7 +233,7 @@ public class DexFile {
}
private static DexWriter outputAt(RandomAccessFile raf, int filePosition) throws IOException {
return new DexWriter(raf.getChannel(), filePosition);
return new DexWriter(new RandomAccessFileOutputStream(raf), filePosition);
}
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {

View File

@ -31,29 +31,19 @@
package org.jf.dexlib2.writer;
import com.google.common.base.Preconditions;
import org.jf.util.ExceptionWithContext;
import javax.annotation.Nonnull;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class DexWriter extends OutputStream {
private static final int MAP_SIZE = 1024*1024;
private static final int BUF_SIZE = 256*1024;
@Nonnull private final FileChannel channel;
private MappedByteBuffer byteBuffer;
/** The position in the file at which byteBuffer starts. */
private int mappedFilePosition;
private byte[] buf = new byte[BUF_SIZE];
/** The index within buf to write to */
private int bufPosition;
public class DexWriter extends BufferedOutputStream {
/**
* The position within the file that we will write to next. This is only updated when the buffer is flushed to the
* outputStream.
*/
private int filePosition;
/**
* A temporary buffer that can be used for larger writes. Can be replaced with a larger buffer if needed.
@ -61,22 +51,29 @@ public class DexWriter extends OutputStream {
*/
private byte[] tempBuf = new byte[8];
/** A buffer of 0s we used for writing alignment values */
/** A buffer of 0s to use for writing alignment values */
private byte[] zeroBuf = new byte[3];
public DexWriter(FileChannel channel, int position) throws IOException {
this.channel = channel;
this.mappedFilePosition = position;
/**
* Construct a new DexWriter instance that writes to output.
*
* @param output An OutputStream to write the data to.
* @param filePosition The position within the file that OutputStream will write to.
*/
public DexWriter(@Nonnull OutputStream output, int filePosition) {
this(output, filePosition, 256 * 1024);
}
byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, position, MAP_SIZE);
public DexWriter(@Nonnull OutputStream output, int filePosition, int bufferSize) {
super(output, bufferSize);
this.filePosition = filePosition;
}
@Override
public void write(int b) throws IOException {
if (bufPosition >= BUF_SIZE) {
flushBuffer();
}
buf[bufPosition++] = (byte)b;
filePosition++;
super.write(b);
}
@Override
@ -86,30 +83,8 @@ public class DexWriter extends OutputStream {
@Override
public void write(byte[] b, int off, int len) throws IOException {
int toWrite = len;
if (bufPosition == BUF_SIZE) {
flushBuffer();
}
int remainingBuffer = BUF_SIZE - bufPosition;
if (toWrite >= remainingBuffer) {
// fill up and write out the current buffer
System.arraycopy(b, 0, buf, bufPosition, remainingBuffer);
bufPosition += remainingBuffer;
toWrite -= remainingBuffer;
flushBuffer();
// skip the intermediate buffer while we have a full buffer's worth
while (toWrite >= BUF_SIZE) {
writeBufferToMap(b, len - toWrite, BUF_SIZE);
toWrite -= BUF_SIZE;
}
}
// write out the final chunk, if any
if (toWrite > 0) {
System.arraycopy(b, len-toWrite, buf, bufPosition, len);
bufPosition += len;
}
filePosition += len;
super.write(b, off, len);
}
public void writeLong(long value) throws IOException {
@ -291,48 +266,7 @@ public class DexWriter extends OutputStream {
}
}
@Override
public void flush() throws IOException {
if (bufPosition > 0) {
writeBufferToMap(buf, 0, bufPosition);
bufPosition = 0;
}
byteBuffer.force();
mappedFilePosition += byteBuffer.position();
channel.position(mappedFilePosition + MAP_SIZE);
channel.write(ByteBuffer.wrap(new byte[]{0}));
byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, mappedFilePosition, MAP_SIZE);
}
@Override
public void close() throws IOException {
if (bufPosition > 0) {
writeBufferToMap(buf, 0, bufPosition);
bufPosition = 0;
}
byteBuffer.force();
byteBuffer = null;
buf = null;
tempBuf = null;
}
private void flushBuffer() throws IOException {
Preconditions.checkState(bufPosition == BUF_SIZE);
writeBufferToMap(buf, 0, BUF_SIZE);
bufPosition = 0;
}
private void writeBufferToMap(byte[] buf, int bufOffset, int len) throws IOException {
// we always write BUF_SIZE, which evenly divides our mapped size, so we only care if remaining is 0 yet
if (!byteBuffer.hasRemaining()) {
byteBuffer.force();
mappedFilePosition += MAP_SIZE;
byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, mappedFilePosition, MAP_SIZE);
}
byteBuffer.put(buf, bufOffset, len);
}
public int getPosition() {
return mappedFilePosition + byteBuffer.position() + bufPosition;
return filePosition;
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.util;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class RandomAccessFileOutputStream extends OutputStream {
@Nonnull private final RandomAccessFile raf;
public RandomAccessFileOutputStream(@Nonnull RandomAccessFile raf) {
this.raf = raf;
}
@Override public void write(int b) throws IOException {
raf.write(b);
}
@Override public void write(byte[] b) throws IOException {
raf.write(b);
}
@Override public void write(byte[] b, int off, int len) throws IOException {
raf.write(b, off, len);
}
}