Changeset 448


Ignore:
Timestamp:
06/03/09 17:17:13 (3 years ago)
Author:
octorian
Message:

Replaced StringBuffer usage in receive() with DataBuffer so that only a single String object is created on return. Added synchronization to some methods just-in-case.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/LogicMail-1.1/LogicMail/src/org/logicprobe/LogicMail/util/Connection.java

    r423 r448  
    7070import net.rim.device.api.ui.UiApplication; 
    7171import net.rim.device.api.ui.component.Dialog; 
     72import net.rim.device.api.util.DataBuffer; 
     73 
    7274import org.logicprobe.LogicMail.AppInfo; 
    7375import org.logicprobe.LogicMail.conf.GlobalConfig; 
     
    113115     */ 
    114116    private byte[] buffer = new byte[128]; 
    115      
     117 
     118    /** 
     119     * Provides a dynamic buffer for building results 
     120     */ 
     121    DataBuffer resultBuffer = new DataBuffer(); 
     122 
    116123    /** 
    117124     * Holds the actual number of bytes in the buffer. 
     
    138145     * Opens a connection. 
    139146     */ 
    140     public void open() throws IOException { 
     147    public synchronized void open() throws IOException { 
    141148        if(input != null || output != null || socket != null) { 
    142149            close(); 
     
    194201     * Closes a connection. 
    195202     */ 
    196     public void close() throws IOException { 
     203    public synchronized void close() throws IOException { 
    197204        try { 
    198205            if(input != null) { 
     
    293300     * @see #receive 
    294301     */ 
    295     public void send(String s) throws IOException { 
     302    public synchronized void send(String s) throws IOException { 
    296303        byte[] bytes = s.getBytes(); 
    297304        int length = bytes.length; 
     
    350357     * is a prepared protocol command. 
    351358     */ 
    352     public void sendCommand(String s) throws IOException { 
     359    public synchronized void sendCommand(String s) throws IOException { 
    353360        if(globalConfig.getConnDebug()) { 
    354361            EventLogger.logEvent(AppInfo.GUID, ("[SEND CMD] " + s).getBytes(), EventLogger.DEBUG_INFO); 
     
    372379     * @see #send 
    373380     */ 
    374     public void sendRaw(String s) throws IOException { 
     381    public synchronized void sendRaw(String s) throws IOException { 
    375382        byte[] bytes = s.getBytes(); 
    376383         
     
    393400     * @see #send 
    394401     */ 
    395     public String receive() throws IOException { 
     402    public synchronized String receive() throws IOException { 
    396403        /** 
    397404         * A note on how this method works and why it is designed the 
     
    421428         * but there's not much one can about that. 
    422429         */ 
    423         StringBuffer resultBuffer = new StringBuffer(); 
    424430        boolean stop = false; 
     431        resultBuffer.reset(); 
    425432         
    426433        /** 
     
    470477                 */ 
    471478                // Note: We really should look for CRLF, and not use this 
    472                 // half-assed approach which screws up on mid-lime LFs. (DK) 
     479                // half-assed approach which screws up on mid-line LFs. (DK) 
    473480                else { 
    474481                    byte b = buffer[count]; 
     
    498505                } 
    499506            } 
    500             resultBuffer.append(new String(buffer, 0, count)); 
    501         } 
     507            resultBuffer.write(buffer, 0, count); 
     508        } 
     509         
     510        String result = new String(resultBuffer.toArray()); 
    502511         
    503512        if(globalConfig.getConnDebug()) { 
    504             EventLogger.logEvent(AppInfo.GUID, ("[RECV] " + resultBuffer.toString()).getBytes(), EventLogger.DEBUG_INFO); 
    505         } 
    506          
    507         return resultBuffer.toString(); 
     513            EventLogger.logEvent(AppInfo.GUID, ("[RECV] " + result).getBytes(), EventLogger.DEBUG_INFO); 
     514        } 
     515         
     516        return result; 
    508517    } 
    509518} 
Note: See TracChangeset for help on using the changeset viewer.