Retrieve the specified message. Given an estimate of the message's size we can be more efficient, preallocating the array and returning a SharedInputStream to allow us to share the array.
(int msg, int size)
| 886 | * us to share the array. |
| 887 | */ |
| 888 | synchronized InputStream retr(int msg, int size) throws IOException { |
| 889 | Response r; |
| 890 | String cmd; |
| 891 | boolean batch = size == 0 && pipelining; |
| 892 | if (batch) { |
| 893 | cmd = "LIST " + msg; |
| 894 | batchCommandStart(cmd); |
| 895 | issueCommand(cmd); |
| 896 | cmd = "RETR " + msg; |
| 897 | batchCommandContinue(cmd); |
| 898 | issueCommand(cmd); |
| 899 | r = readResponse(); |
| 900 | if (r.ok && r.data != null) { |
| 901 | // parse the LIST response to get the message size |
| 902 | try { |
| 903 | StringTokenizer st = new StringTokenizer(r.data); |
| 904 | st.nextToken(); // skip message number |
| 905 | size = Integer.parseInt(st.nextToken()); |
| 906 | // don't allow ridiculous sizes |
| 907 | if (size > 1024*1024*1024 || size < 0) |
| 908 | size = 0; |
| 909 | else { |
| 910 | if (logger.isLoggable(Level.FINE)) |
| 911 | logger.fine("pipeline message size " + size); |
| 912 | size += SLOP; |
| 913 | } |
| 914 | } catch (RuntimeException e) { |
| 915 | } |
| 916 | } |
| 917 | r = readResponse(); |
| 918 | if (r.ok) |
| 919 | r.bytes = readMultilineResponse(size + SLOP); |
| 920 | batchCommandEnd(); |
| 921 | } else { |
| 922 | cmd = "RETR " + msg; |
| 923 | multilineCommandStart(cmd); |
| 924 | issueCommand(cmd); |
| 925 | r = readResponse(); |
| 926 | if (!r.ok) { |
| 927 | multilineCommandEnd(); |
| 928 | return null; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Many servers return a response to the RETR command of the form: |
| 933 | * +OK 832 octets |
| 934 | * If we don't have a size guess already, try to parse the response |
| 935 | * for data in that format and use it if found. It's only a guess, |
| 936 | * but it might be a good guess. |
| 937 | */ |
| 938 | if (size <= 0 && r.data != null) { |
| 939 | try { |
| 940 | StringTokenizer st = new StringTokenizer(r.data); |
| 941 | String s = st.nextToken(); |
| 942 | String octets = st.nextToken(); |
| 943 | if (octets.equals("octets")) { |
| 944 | size = Integer.parseInt(s); |
| 945 | // don't allow ridiculous sizes |
no test coverage detected