|
7 | 7 |
|
8 | 8 | public abstract class MultipartUtils { |
9 | 9 |
|
10 | | - // copied from com.github.scribejava.core.httpclient.jdk.JDKHttpClient#getPayload |
11 | | - public static ByteArrayOutputStream getPayload(MultipartPayload multipartPayload) throws IOException { |
12 | | - final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 10 | + // copied from com.github.scribejava.core.httpclient.jdk.JDKHttpClient#getPayload |
| 11 | + public static ByteArrayOutputStream getPayload(MultipartPayload multipartPayload) throws IOException { |
| 12 | + final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
13 | 13 |
|
14 | | - final String preamble = multipartPayload.getPreamble(); |
15 | | - if (preamble != null) { |
16 | | - os.write(preamble.getBytes()); |
17 | | - } |
18 | | - final List<BodyPartPayload> bodyParts = multipartPayload.getBodyParts(); |
19 | | - if (!bodyParts.isEmpty()) { |
20 | | - final String boundary = multipartPayload.getBoundary(); |
21 | | - final byte[] startBoundary = ("\r\n--" + boundary + "\r\n").getBytes(); |
22 | | - |
23 | | - for (BodyPartPayload bodyPart : bodyParts) { |
24 | | - os.write(startBoundary); |
25 | | - |
26 | | - final Map<String, String> bodyPartHeaders = bodyPart.getHeaders(); |
27 | | - if (bodyPartHeaders != null) { |
28 | | - for (Map.Entry<String, String> header : bodyPartHeaders.entrySet()) { |
29 | | - os.write((header.getKey() + ": " + header.getValue() + "\r\n").getBytes()); |
30 | | - } |
| 14 | + final String preamble = multipartPayload.getPreamble(); |
| 15 | + if (preamble != null) { |
| 16 | + os.write(preamble.getBytes()); |
31 | 17 | } |
| 18 | + final List<BodyPartPayload> bodyParts = multipartPayload.getBodyParts(); |
| 19 | + if (!bodyParts.isEmpty()) { |
| 20 | + final String boundary = multipartPayload.getBoundary(); |
| 21 | + final byte[] startBoundary = ("\r\n--" + boundary + "\r\n").getBytes(); |
| 22 | + |
| 23 | + for (BodyPartPayload bodyPart : bodyParts) { |
| 24 | + os.write(startBoundary); |
| 25 | + |
| 26 | + final Map<String, String> bodyPartHeaders = bodyPart.getHeaders(); |
| 27 | + if (bodyPartHeaders != null) { |
| 28 | + for (Map.Entry<String, String> header : bodyPartHeaders.entrySet()) { |
| 29 | + os.write((header.getKey() + ": " + header.getValue() + "\r\n").getBytes()); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if (bodyPart instanceof MultipartPayload) { |
| 34 | + getPayload((MultipartPayload) bodyPart).writeTo(os); |
| 35 | + } else if (bodyPart instanceof ByteArrayBodyPartPayload) { |
| 36 | + os.write("\r\n".getBytes()); |
| 37 | + os.write(((ByteArrayBodyPartPayload) bodyPart).getPayload()); |
| 38 | + } else { |
| 39 | + throw new AssertionError(bodyPart.getClass()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + os.write(("\r\n--" + boundary + "--\r\n").getBytes()); |
| 44 | + final String epilogue = multipartPayload.getEpilogue(); |
| 45 | + if (epilogue != null) { |
| 46 | + os.write((epilogue + "\r\n").getBytes()); |
| 47 | + } |
32 | 48 |
|
33 | | - if (bodyPart instanceof MultipartPayload) { |
34 | | - getPayload((MultipartPayload) bodyPart).writeTo(os); |
35 | | - } else if (bodyPart instanceof ByteArrayBodyPartPayload) { |
36 | | - os.write("\r\n".getBytes()); |
37 | | - os.write(((ByteArrayBodyPartPayload) bodyPart).getPayload()); |
38 | | - } else { |
39 | | - throw new AssertionError(bodyPart.getClass()); |
40 | 49 | } |
41 | | - } |
42 | | - |
43 | | - os.write(("\r\n--" + boundary + "--\r\n").getBytes()); |
44 | | - final String epilogue = multipartPayload.getEpilogue(); |
45 | | - if (epilogue != null) { |
46 | | - os.write((epilogue + "\r\n").getBytes()); |
47 | | - } |
48 | | - |
| 50 | + return os; |
49 | 51 | } |
50 | | - return os; |
51 | | - } |
52 | 52 |
|
53 | 53 | } |
0 commit comments