Skip to content

Commit

Permalink
superduperspeedgogoggo
Browse files Browse the repository at this point in the history
  • Loading branch information
DenWav committed Sep 25, 2016
1 parent d7690fe commit d7ab8f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/destroystokyo/paperclip/Paperclip.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public static void main(String[] args) {
final byte[] vanillaJarBytes;
final byte[] patch;
try {

vanillaJarBytes = getBytes(vanillaJar);
patch = Utils.readFully(patchInfo.getPatchFile().openStream());
} catch (IOException e) {
Expand Down
31 changes: 12 additions & 19 deletions src/main/java/com/destroystokyo/paperclip/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@

package com.destroystokyo.paperclip;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
import java.util.Arrays;

class Utils {

Expand All @@ -37,19 +31,18 @@ static byte[] fromHex(String s) {

static byte[] readFully(InputStream in) throws IOException {
try {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final ReadableByteChannel readChannel = Channels.newChannel(in);
final WritableByteChannel writeChannel = Channels.newChannel(stream);

final ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);

while (readChannel.read(buffer) >= 0 || buffer.position() > 0) {
buffer.flip();
writeChannel.write(buffer);
buffer.compact();
// In a test this was 12 ms quicker than a ByteBuffer
// and for some reason that matters here.
byte[] buffer = new byte[16 * 1024];
int off = 0;
int read;
while ((read = in.read(buffer, off, buffer.length - off)) != -1) {
off += read;
if (off == buffer.length) {
buffer = Arrays.copyOf(buffer, buffer.length * 2);
}
}

return stream.toByteArray();
return Arrays.copyOfRange(buffer, 0, off);
} finally {
in.close();
}
Expand Down

0 comments on commit d7ab8f9

Please sign in to comment.