Ticket #2496: videoPerformance_java.patch
File videoPerformance_java.patch, 10.5 KB (added by boyanl, 15 years ago) |
---|
-
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/RawBinData.java
### Eclipse Workspace Patch 1.0 #P sophie
10 10 11 11 import org.sophie2.base.commons.util.Base64; 12 12 import org.sophie2.core.prolib.annot.Immutable; 13 import org.sophie2.base.commons.util.bindata.FastByteArrayOutputStream; 13 14 14 15 /** 15 16 * Immutable binary data. … … 115 116 */ 116 117 public RawBinData(InputStream input, int size) throws IOException { 117 118 // should read exactly size bytes 118 ByteArrayOutputStream os = new ByteArrayOutputStream();119 FastByteArrayOutputStream os = new FastByteArrayOutputStream(size); 119 120 transport(input, os, size); 120 121 this.data = os.toByteArray(); 121 122 } … … 137 138 138 139 @Override 139 140 public byte[] getBytes() { 140 byte[] copyOfData = new byte[this.data.length]; 141 System.arraycopy(this.data, 0, copyOfData, 0, this.data.length); 142 return copyOfData; 141 return this.data; 143 142 } 144 143 145 144 -
modules/org.sophie2.main.media.natlib/src/main/java/org/sophie2/main/media/natlib/decoder/NativeMediaHandler.java
106 106 // If there are some requests for AudioChunks... 107 107 if (!reqQueue.isEmpty()) { 108 108 lastRequest = reqQueue.poll(); 109 SophieLog.debug("Fetching request for position " + lastRequest);109 //SophieLog.debug("Fetching request for position " + lastRequest); 110 110 111 111 // If the requested chunk is not in the cache retrieve it 112 112 // from the natives and put it in the cache. … … 135 135 136 136 //Prefetch: 137 137 if (nextNeeded != -1) { 138 SophieLog.debug("Prefetching: " + nextNeeded);138 //SophieLog.debug("Prefetching: " + nextNeeded); 139 139 this.audioCache.put(nextNeeded, getNativeAudio(nextNeeded)); 140 140 } else { 141 141 // If there is no request yet just sleep for a while... … … 249 249 this.lastFrame = getVideoBridge().getFrame((int) millis % getModLen()); 250 250 assert this.lastFrame != null; 251 251 this.lastFrameMillis = millis; 252 252 253 return this.lastFrame; 253 254 } 254 255 -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinReader.java
23 23 public final class BinReader { 24 24 private final BinData data; 25 25 private int pos; 26 private byte[] bytes; 26 27 27 28 /** 28 29 * Constructor … … 31 32 public BinReader(BinData data) { 32 33 this.data = data; 33 34 this.pos = 0; 35 36 /* 37 * Small optimization - instead of performing a virtual call to read each byte, 38 * fetch them all at once at construction and then simply index 39 * We force the data to be kept in memory but RawBinData is stored in main memory anyways 40 */ 41 this.bytes = data.getBytes(); 34 42 } 35 43 36 44 /** … … 91 99 private byte readByte() { 92 100 assert this.pos < this.data.getSize() : 93 101 "pos=" + this.pos + " ,size=" + this.data.getSize(); 94 return this.data.getByte(this.pos++); 102 103 return this.bytes[this.pos++]; 95 104 } 96 105 97 106 private byte[] readArray(int len) { 98 107 // TODO may be written faster if needed 99 108 byte[] res = new byte[len]; 100 for (int i = 0; i < len; ++i) { 101 res[i] = readByte(); 102 } 109 //copy bytes [pos, pos+len) 110 System.arraycopy(this.bytes, this.pos, res, 0, len); 111 this.pos += len; 112 103 113 return res; 104 114 } 105 115 -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinWriter.java
37 37 } 38 38 this.data[this.size++] = b; 39 39 } 40 41 private void addBytes(byte[] b) { 42 if (this.size + b.length > this.data.length) { 43 /* 44 * We want to set the new size to the closest power of 2, which is >= this.size + this.length 45 * Instead of looping through the powers and checking we do it via some bitwise operations 46 */ 47 int newLen = this.size + b.length - 1; 48 newLen |= newLen >> 1; 49 newLen |= newLen >> 2; 50 newLen |= newLen >> 4; 51 newLen |= newLen >> 8; 52 newLen |= newLen >> 16; 53 ++newLen; 54 55 byte[] oldData = this.data; 56 byte[] newData = new byte[newLen]; 57 System.arraycopy(oldData, 0, newData, 0, this.size); 58 this.data = newData; 59 } 60 System.arraycopy (b, 0, this.data, this.size, b.length); 61 this.size += b.length; 62 } 40 63 41 64 /** 42 65 * Writes an encoded int. … … 56 79 public void writeBinData(BinData value) { 57 80 writeInt(value.getSize()); 58 81 byte[] bytes = value.getBytes(); 59 for (int i = 0; i < value.getSize(); ++i) { 60 addByte(bytes[i]); 61 } 82 addBytes (bytes); 62 83 } 63 84 64 85 /** -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/FastByteArrayOutputStream.java
1 package org.sophie2.base.commons.util.bindata; 2 3 import java.io.OutputStream; 4 5 /** 6 * ByteArrayOutputStream implementation that doesn't synchronize methods 7 * and doesn't copy the data on toByteArray(). 8 */ 9 public class FastByteArrayOutputStream extends OutputStream { 10 /** 11 * The buffer 12 */ 13 protected byte[] buf = null; 14 /** 15 * The size of actual data written in the buffer 16 */ 17 protected int size = 0; 18 19 /** 20 * Constructs a stream with buffer capacity size 8K 21 */ 22 public FastByteArrayOutputStream() { 23 this(8 * 1024); 24 } 25 26 /** 27 * Constructs a stream with the given initial size 28 * @param initSize 29 * The given initial size 30 */ 31 public FastByteArrayOutputStream(int initSize) { 32 this.size = 0; 33 this.buf = new byte[initSize]; 34 } 35 36 /** 37 * Ensures that we have a large enough buffer for the given size. 38 * Marked final for easier inlining 39 */ 40 private final void verifyBufferSize(int sz) { 41 if (sz > this.buf.length) { 42 byte[] old = this.buf; 43 this.buf = new byte[Math.max(sz, 2 * this.buf.length )]; 44 System.arraycopy(old, 0, this.buf, 0, old.length); 45 old = null; 46 } 47 } 48 49 /** 50 * @return 51 * Returns the size of the written data. 52 */ 53 public int getSize() { 54 return this.size; 55 } 56 57 /** 58 * @return 59 * Returns the byte array containing the written data. Note that this 60 * array will almost always be larger than the amount of data actually 61 * written. Thus you should use the [0, size) interval of the returned array 62 */ 63 public byte[] toByteArray() { 64 return this.buf; 65 } 66 67 @Override 68 public final void write(byte b[]) { 69 verifyBufferSize(this.size + b.length); 70 System.arraycopy(b, 0, this.buf, this.size, b.length); 71 this.size += b.length; 72 } 73 74 @Override 75 public final void write(byte b[], int off, int len) { 76 verifyBufferSize(this.size + len); 77 System.arraycopy(b, off, this.buf, this.size, len); 78 this.size += len; 79 } 80 81 @Override 82 public final void write(int b) { 83 verifyBufferSize(this.size + 1); 84 this.buf[this.size++] = (byte) b; 85 } 86 87 /** 88 * Resets the size counter of the data, effectively writing over the old data when you 89 * perform writes. 90 */ 91 public void reset() { 92 this.size = 0; 93 } 94 } -
modules/org.sophie2.extra.func.spellcheck/src/test/java/org/sophie2/extra/func/spellcheck/SpellCheckDemoTest.java
327 327 String textStr = modelText.toString(); 328 328 329 329 List<String> misspelled = new LinkedList<String> (); 330 //SpellCheckUtility.getMisspelledWords (misspelled, null, textStr);330 SpellCheckUtility.getMisspelledWords (misspelled, null, textStr, null); 331 331 332 332 DefaultListModel model = (DefaultListModel) listMisspelled.getModel(); 333 333 -
modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/BinData.java
126 126 * if occurs :) 127 127 */ 128 128 public static void transport(InputStream from, OutputStream to) throws IOException { 129 byte[] buffer = new byte[1 << 1 4];129 byte[] buffer = new byte[1 << 16]; 130 130 int read; 131 131 while ((read = from.read(buffer)) != -1) { 132 132 to.write(buffer, 0, read); … … 144 144 * if occurs 145 145 */ 146 146 public static void transport(Reader reader, Writer writer) throws IOException { 147 char[] buffer = new char[1 << 1 4];147 char[] buffer = new char[1 << 16]; 148 148 int read; 149 149 while ((read = reader.read(buffer)) != -1) { 150 150 writer.write(buffer, 0, read); … … 169 169 */ 170 170 protected void transport(InputStream from, OutputStream to, int size) throws IOException { 171 171 assert size > 0; 172 byte[] buffer = new byte[1 << 1 4];172 byte[] buffer = new byte[1 << 16]; 173 173 int remaining = size; 174 174 int read; 175 175 while ((read = from.read(buffer, 0, Math.min(remaining, buffer.length))) != -1