Skip to content

Commit

Permalink
Add ability to write to OutputStream.
Browse files Browse the repository at this point in the history
Lots of refactoring involved and adding test cases.
Each chunk/section/resouce should now be able to
properly use a toByte() function with approapriate
getSize() calls.

However I do need to double back and check the
style pool inside the string section.`
  • Loading branch information
strazzere committed Oct 1, 2015
1 parent 3ace0dd commit 96fc9fc
Show file tree
Hide file tree
Showing 28 changed files with 1,172 additions and 143 deletions.
92 changes: 63 additions & 29 deletions src/main/java/android/content/res/AXMLResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
*/
package android.content.res;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashSet;

import android.content.res.chunk.ChunkType;
import android.content.res.chunk.ChunkUtil;
import android.content.res.chunk.sections.ResourceSection;
import android.content.res.chunk.sections.StringSection;
import android.content.res.chunk.types.AXMLHeader;
import android.content.res.chunk.types.Chunk;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Iterator;
import java.util.LinkedHashSet;

/**
* Main AXMLResource object
*
*
* @author tstrazzere
*/
public class AXMLResource {
Expand Down Expand Up @@ -62,29 +65,31 @@ public boolean read(InputStream stream) throws IOException {
Chunk chunk = ChunkUtil.createChunk(reader);

switch (chunk.getChunkType()) {
case AXML_HEADER:
header = (AXMLHeader) chunk;
size = header.getSize();
break;
case STRING_SECTION:
stringSection = (StringSection) chunk;
break;
// operational = true;
case RESOURCE_SECTION:
resourceSection = (ResourceSection) chunk;
break;
case START_NAMESPACE:
case END_NAMESPACE:
case START_TAG:
case END_TAG:
case TEXT_TAG:
chunks.add(chunk);
break;
case BUFFER:
// Do nothing right now, not even add it to the chunk stuff
break;
default:
throw new IOException("Hit an unknown chunk type!");
case AXML_HEADER:
header = (AXMLHeader) chunk;
// TODO : This should warn if true
// This will cause breakages if the header is lying
//size = header.getSize();
break;
case STRING_SECTION:
stringSection = (StringSection) chunk;
break;
// operational = true;
case RESOURCE_SECTION:
resourceSection = (ResourceSection) chunk;
break;
case START_NAMESPACE:
case END_NAMESPACE:
case START_TAG:
case END_TAG:
case TEXT_TAG:
chunks.add(chunk);
break;
case BUFFER:
// Do nothing right now, not even add it to the chunk stuff
break;
default:
throw new IOException("Hit an unknown chunk type!");
}
}

Expand All @@ -98,6 +103,35 @@ public boolean read(InputStream stream) throws IOException {
return false;
}

public void write(OutputStream outputStream) throws IOException {

int chunkSizes = 0;
Iterator<Chunk> iterator = chunks.iterator();
while (iterator.hasNext()) {
Chunk chunk = iterator.next();
chunkSizes += chunk.getSize();
}

System.out.println("String section size: " + stringSection.getSize());
System.out.println("Resource section size: " + resourceSection.getSize());
System.out.println("chunk section size: " + chunkSizes);
System.out.println("Size was " + ((2 * 4) + stringSection.getSize() + resourceSection.getSize() + chunkSizes));

outputStream.write(ByteBuffer.allocate(8)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(ChunkType.AXML_HEADER.getIntType())
.putInt(((2 * 4) + stringSection.getSize() + resourceSection.getSize() + chunkSizes))
.array());
outputStream.write(stringSection.toBytes());
outputStream.write(resourceSection.toBytes());
iterator = chunks.iterator();
while (iterator.hasNext()) {
Chunk chunk = iterator.next();
outputStream.write(chunk.toBytes());
}

}

public void print() {

log("%s", header.toXML(stringSection, resourceSection, 0));
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/android/content/res/IntReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

/**
* Simple helper class that allows reading of integers.
*
* <p>
* TODO: implement buffering
*
*
* @author Dmitry Skiba
*/
public class IntReader {
Expand All @@ -37,11 +37,9 @@ public IntReader(InputStream stream, boolean bigEndian) {

/**
* Reset the POJO to use a new stream.
*
* @param newStream
* the {@code InputStream} to use
* @param isBigEndian
* a boolean for whether or not the stream is in Big Endian format
*
* @param newStream the {@code InputStream} to use
* @param isBigEndian a boolean for whether or not the stream is in Big Endian format
*/
public void reset(InputStream newStream, boolean isBigEndian) {
stream = newStream;
Expand Down Expand Up @@ -77,9 +75,8 @@ public int readInt() throws IOException {

/**
* Read an integer of a certain length from the current stream.
*
* @param length
* to read
*
* @param length to read
* @return
* @throws IOException
*/
Expand Down Expand Up @@ -110,13 +107,12 @@ public int readInt(int length) throws IOException {
}
}

// bytesRead += length;
return result;
}

/**
* Skip a specific number of bytes in the stream.
*
*
* @param bytes
* @throws IOException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/android/content/res/chunk/AttributeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* Enum for attribute types for ChunkTypes
*
*
* @author tstrazzere
*/
public enum AttributeType {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/android/content/res/chunk/ChunkType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* Enum for ChunkTypes - the different types of Chunks available to create
*
*
* @author tstrazzere
*/
public enum ChunkType {
Expand Down
52 changes: 23 additions & 29 deletions src/main/java/android/content/res/chunk/ChunkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@
*/
package android.content.res.chunk;

import java.io.IOException;

import android.content.res.IntReader;
import android.content.res.chunk.sections.ResourceSection;
import android.content.res.chunk.sections.StringSection;
import android.content.res.chunk.types.AXMLHeader;
import android.content.res.chunk.types.Buffer;
import android.content.res.chunk.types.Chunk;
import android.content.res.chunk.types.EndTag;
import android.content.res.chunk.types.NameSpace;
import android.content.res.chunk.types.StartTag;
import android.content.res.chunk.types.TextTag;
import android.content.res.chunk.types.*;

import java.io.IOException;

/**
* Simple class for reading chunk types.
*
*
* @author tstrazzere
*/
public class ChunkUtil {
Expand All @@ -52,25 +46,25 @@ public static Chunk createChunk(IntReader reader) throws IOException {
ChunkType chunkType = readChunkType(reader);

switch (chunkType) {
case AXML_HEADER:
return new AXMLHeader(chunkType, reader);
case STRING_SECTION:
return new StringSection(chunkType, reader);
case RESOURCE_SECTION:
return new ResourceSection(chunkType, reader);
case START_NAMESPACE:
case END_NAMESPACE:
return new NameSpace(chunkType, reader);
case START_TAG:
return new StartTag(chunkType, reader);
case END_TAG:
return new EndTag(chunkType, reader);
case TEXT_TAG:
return new TextTag(chunkType, reader);
case BUFFER:
return new Buffer(chunkType, reader);
default:
throw new IOException("Unexpected tag!");
case AXML_HEADER:
return new AXMLHeader(chunkType, reader);
case STRING_SECTION:
return new StringSection(chunkType, reader);
case RESOURCE_SECTION:
return new ResourceSection(chunkType, reader);
case START_NAMESPACE:
case END_NAMESPACE:
return new NameSpace(chunkType, reader);
case START_TAG:
return new StartTag(chunkType, reader);
case END_TAG:
return new EndTag(chunkType, reader);
case TEXT_TAG:
return new TextTag(chunkType, reader);
case BUFFER:
return new Buffer(chunkType, reader);
default:
throw new IOException("Unexpected tag!");
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/android/content/res/chunk/PoolItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* Simple POJO for keeping the offsets and data for items inside of "pools".
*
*
* @author tstrazzere
*/
public class PoolItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package android.content.res.chunk.sections;

import java.io.IOException;

import android.content.res.IntReader;
import android.content.res.chunk.types.Chunk;

import java.io.IOException;

/**
* Interface for Chunk which is a section type
*
*
* @author tstrazzere
*/
public interface ChunkSection extends Chunk {
Expand All @@ -37,7 +37,7 @@ public interface ChunkSection extends Chunk {

/**
* Read the
*
*
* @param inputReader
* @throws IOException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package android.content.res.chunk.sections;

import java.io.IOException;

import android.content.res.IntReader;
import android.content.res.chunk.ChunkType;
import android.content.res.chunk.types.Chunk;
import android.content.res.chunk.types.GenericChunk;

import java.io.IOException;

/**
* Generic ChunkSection class for generalizing the reading and minimizing the repetitive code inside of the specific
* sections (likely overkill..)
*
*
* @author tstrazzere
*/
public abstract class GenericChunkSection extends GenericChunk implements Chunk, ChunkSection {
Expand Down
Loading

0 comments on commit 96fc9fc

Please sign in to comment.