Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.commons.net.imap;
 
 import java.util.List;
 
The IMAP class provides the basic the functionality necessary to implement your own IMAP client.
 
 public class IMAP extends SocketClient
 {
    
The default IMAP port (RFC 3501).
 
     public static final int DEFAULT_PORT = 143;
 
     public enum IMAPState
     {
        
A constant representing the state where the client is not yet connected to a server.
 
         DISCONNECTED_STATE,
        
A constant representing the "not authenticated" state.
 
         NOT_AUTH_STATE,
        
A constant representing the "authenticated" state.
 
         AUTH_STATE,
        
A constant representing the "logout" state.
 
         LOGOUT_STATE;
     }
 
     // RFC 3501, section 5.1.3. It should be "modified UTF-7".
     
The default control socket ecoding.
 
     protected static final String __DEFAULT_ENCODING = "ISO-8859-1";
 
     private IMAPState __state;
     protected BufferedWriter __writer;
 
     protected BufferedReader _reader;
     private int _replyCode;
     private final List<String_replyLines;
 
     private final char[] _initialID = { 'A''A''A''A' };

    
The default IMAPClient constructor. Initializes the state to DISCONNECTED_STATE.
 
     public IMAP()
     {
         setDefaultPort();
          = .;
          = null;
          = null;
          = new ArrayList<String>();
         createCommandSupport();
     }

    
Get the reply for a command that expects a tagged response.

 
     private void __getReply() throws IOException
     {
         __getReply(true); // tagged response
     }

    
Get the reply for a command, reading the response until the reply is found.

Parameters:
wantTag true if the command expects a tagged response.
Throws:
java.io.IOException
 
    private void __getReply(boolean wantTagthrows IOException
    {
        .clear();
        String line = .readLine();
        if (line == null) {
            throw new EOFException("Connection closed without indication.");
        }
        .add(line);
        if (wantTag) {
            while(IMAPReply.isUntagged(line)) {
                int literalCount = IMAPReply.literalCount(line);
                while (literalCount >= 0) {
                    line=.readLine();
                    if (line == null) {
                        throw new EOFException("Connection closed without indication.");
                    }
                    .add(line);
                    literalCount -= (line.length() + 2); // Allow for CRLF
                }
                line = .readLine();
                if (line == null) {
                    throw new EOFException("Connection closed without indication.");
                }
                .add(line);
            }
            // check the response code on the last line
             = IMAPReply.getReplyCode(line);
        } else {
             = IMAPReply.getUntaggedReplyCode(line);
        }
    }

    
Performs connection initialization and sets state to IMAP.IMAPState.NOT_AUTH_STATE.
    @Override
    protected void _connectAction_() throws IOException
    {
        super._connectAction_();
         =
          new CRLFLineReader(new InputStreamReader(,
                                                   ));
         =
          new BufferedWriter(new OutputStreamWriter(,
                                                    ));
        int tmo = getSoTimeout();
        if (tmo <= 0) { // none set currently
            setSoTimeout(); // use connect timeout to ensure we don't block forever
        }
        __getReply(false); // untagged response
        if (tmo <= 0) {
            setSoTimeout(tmo); // restore the original value
        }
    }

    
Sets IMAP client state. This must be one of the _STATE constants.

Parameters:
state The new state.
    protected void setState(IMAP.IMAPState state)
    {
         = state;
    }


    
Returns the current IMAP client state.

Returns:
The current IMAP client state.
    public IMAP.IMAPState getState()
    {
        return ;
    }

    
Disconnects the client from the server, and sets the state to DISCONNECTED_STATE . The reply text information from the last issued command is voided to allow garbage collection of the memory used to store that information.

Throws:
java.io.IOException If there is an error in disconnecting.
    @Override
    public void disconnect() throws IOException
    {
        super.disconnect();
         = null;
         = null;
        .clear();
    }


    
Sends a command an arguments to the server and returns the reply code.

Parameters:
commandID The ID (tag) of the command.
command The IMAP command to send.
args The command arguments.
Returns:
The server reply code (either IMAPReply.OK, IMAPReply.NO or IMAPReply.BAD).
    private int sendCommandWithID(String commandIDString commandString argsthrows IOException
    {
        StringBuilder __commandBuffer = new StringBuilder();
        if (commandID != null)
        {
            __commandBuffer.append(commandID);
            __commandBuffer.append(' ');
        }
        __commandBuffer.append(command);
        if (args != null)
        {
            __commandBuffer.append(' ');
            __commandBuffer.append(args);
        }
        __commandBuffer.append(.);
        String message = __commandBuffer.toString();
        .write(message);
        .flush();
        fireCommandSent(commandmessage);
        __getReply();
        return ;
    }

    
Sends a command an arguments to the server and returns the reply code.

Parameters:
command The IMAP command to send.
args The command arguments.
Returns:
The server reply code (see IMAPReply).
    public int sendCommand(String commandString argsthrows IOException
    {
        return sendCommandWithID(generateCommandID(), commandargs);
    }

    
Sends a command with no arguments to the server and returns the reply code.

Parameters:
command The IMAP command to send.
Returns:
The server reply code (see IMAPReply).
    public int sendCommand(String commandthrows IOException
    {
        return sendCommand(commandnull);
    }

    
Sends a command and arguments to the server and returns the reply code.

Parameters:
command The IMAP command to send (one of the IMAPCommand constants).
args The command arguments.
Returns:
The server reply code (see IMAPReply).
    public int sendCommand(IMAPCommand commandString argsthrows IOException
    {
        return sendCommand(command.getIMAPCommand(), args);
    }

    
Sends a command and arguments to the server and return whether successful.

Parameters:
command The IMAP command to send (one of the IMAPCommand constants).
args The command arguments.
Returns:
true if the command was successful
    public boolean doCommand(IMAPCommand commandString argsthrows IOException
    {
        return IMAPReply.isSuccess(sendCommand(commandargs));
    }

    
Sends a command with no arguments to the server and returns the reply code.

Parameters:
command The IMAP command to send (one of the IMAPCommand constants).
Returns:
The server reply code (see IMAPReply).
    public int sendCommand(IMAPCommand commandthrows IOException
    {
        return sendCommand(commandnull);
    }

    
Sends a command to the server and return whether successful.

Parameters:
command The IMAP command to send (one of the IMAPCommand constants).
Returns:
true if the command was successful
    public boolean doCommand(IMAPCommand commandthrows IOException
    {
        return IMAPReply.isSuccess(sendCommand(command));
    }

    
Sends data to the server and returns the reply code.

Parameters:
command The IMAP command to send.
Returns:
The server reply code (see IMAPReply).
    public int sendData(String commandthrows IOException
    {
        return sendCommandWithID(nullcommandnull);
    }

    
Returns an array of lines received as a reply to the last command sent to the server. The lines have end of lines truncated.

Returns:
The last server response.
    public String[] getReplyStrings()
    {
        return .toArray(new String[.size()]);
    }

    
Returns the reply to the last command sent to the server. The value is a single string containing all the reply lines including newlines.

Returns:
The last server response.
    public String getReplyString()
    {
        StringBuilder buffer = new StringBuilder(256);
        for (String s : )
        {
            buffer.append(s);
            buffer.append(.);
        }
        return buffer.toString();
    }

    
Generates a new command ID (tag) for a command.

Returns:
a new command ID (tag) for an IMAP command.
    protected String generateCommandID()
    {
        String res = new String ();
        // "increase" the ID for the next call
        boolean carry = true// want to increment initially
        for (int i = .-1; carry && i>=0; i--)
        {
            if ([i] == 'Z')
            {
                [i] = 'A';
            }
            else
            {
                [i]++;
                carry = false// did not wrap round
            }
        }
        return res;
    }
/* kate: indent-width 4; replace-tabs on; */
New to GrepCode? Check out our FAQ X