|
| 1 | + |
| 2 | +package com.ning.http.multipart; |
| 3 | + |
| 4 | +import com.ning.http.client.RandomAccessBody; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.RandomAccessFile; |
| 10 | +import java.nio.ByteBuffer; |
| 11 | +import java.nio.channels.FileChannel; |
| 12 | +import java.nio.channels.WritableByteChannel; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class MultipartBody implements RandomAccessBody { |
| 17 | + |
| 18 | + public MultipartBody(List<com.ning.http.client.Part> parts, String boundary, String contentLength) { |
| 19 | + _boundary = MultipartEncodingUtil.getAsciiBytes(boundary.substring("multipart/form-data; boundary=".length())); |
| 20 | + _contentLength = Long.parseLong(contentLength); |
| 21 | + _parts = parts; |
| 22 | + |
| 23 | + _files = new ArrayList<RandomAccessFile>(); |
| 24 | + |
| 25 | + _startPart = 0; |
| 26 | + } |
| 27 | + |
| 28 | + public void close() throws IOException { |
| 29 | + for(RandomAccessFile file : _files) { |
| 30 | + file.close(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public long getContentLength() { |
| 35 | + return _contentLength; |
| 36 | + } |
| 37 | + |
| 38 | + public long read(ByteBuffer buffer) throws IOException { |
| 39 | + // TODO Not implemented |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + public long transferTo(long position, long count, WritableByteChannel target) |
| 44 | + throws IOException { |
| 45 | + |
| 46 | + long overallLength = 0; |
| 47 | + |
| 48 | + if(_startPart == _parts.size()) { |
| 49 | + return overallLength; |
| 50 | + } |
| 51 | + |
| 52 | + long availableLength = count; |
| 53 | + |
| 54 | + int tempPart = _startPart; |
| 55 | + long totalLength = 0; |
| 56 | + boolean full = false; |
| 57 | + |
| 58 | + while(!full && tempPart < _parts.size()) { |
| 59 | + Part currentPart = (Part) _parts.get(tempPart); |
| 60 | + |
| 61 | + currentPart.setPartBoundary(_boundary); |
| 62 | + |
| 63 | + long length = currentPart.length(); |
| 64 | + |
| 65 | + if((length + totalLength) < availableLength ) { |
| 66 | + totalLength += length; |
| 67 | + tempPart++; |
| 68 | + |
| 69 | + if(currentPart.getClass().equals(StringPart.class)) { |
| 70 | + |
| 71 | + ByteArrayOutputStream outputStream = |
| 72 | + new ByteArrayOutputStream(); |
| 73 | + |
| 74 | + Part.sendPart(outputStream, currentPart, _boundary); |
| 75 | + |
| 76 | + overallLength += writeToTarget(target, outputStream); |
| 77 | + } |
| 78 | + else if(currentPart.getClass().equals(FilePart.class)) { |
| 79 | + |
| 80 | + FilePart filePart = (FilePart)currentPart; |
| 81 | + |
| 82 | + ByteArrayOutputStream overhead = |
| 83 | + new ByteArrayOutputStream(); |
| 84 | + |
| 85 | + filePart.setPartBoundary(_boundary); |
| 86 | + |
| 87 | + filePart.sendStart(overhead); |
| 88 | + filePart.sendDispositionHeader(overhead); |
| 89 | + filePart.sendContentTypeHeader(overhead); |
| 90 | + filePart.sendTransferEncodingHeader(overhead); |
| 91 | + filePart.sendEndOfHeader(overhead); |
| 92 | + |
| 93 | + overallLength += writeToTarget(target, overhead); |
| 94 | + |
| 95 | + FilePartSource source = (FilePartSource)filePart.getSource(); |
| 96 | + |
| 97 | + File file = source.getFile(); |
| 98 | + |
| 99 | + RandomAccessFile raf = new RandomAccessFile(file, "r"); |
| 100 | + _files.add(raf); |
| 101 | + |
| 102 | + FileChannel fc = raf.getChannel(); |
| 103 | + |
| 104 | + |
| 105 | + long fileLength = fc.transferTo(0, file.length(), target); |
| 106 | + |
| 107 | + if(fileLength != file.length()) { |
| 108 | + System.out.println("Did not complete file."); |
| 109 | + } |
| 110 | + |
| 111 | + ByteArrayOutputStream endOverhead = |
| 112 | + new ByteArrayOutputStream(); |
| 113 | + |
| 114 | + filePart.sendEnd(endOverhead); |
| 115 | + |
| 116 | + overallLength += this.writeToTarget(target, endOverhead); |
| 117 | + } |
| 118 | + } |
| 119 | + else { |
| 120 | + full = true; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + ByteArrayOutputStream endWriter = |
| 125 | + new ByteArrayOutputStream(); |
| 126 | + |
| 127 | + Part.sendMessageEnd(endWriter, _boundary); |
| 128 | + |
| 129 | + overallLength += writeToTarget(target, endWriter); |
| 130 | + |
| 131 | + _startPart = tempPart; |
| 132 | + |
| 133 | + return overallLength; |
| 134 | + } |
| 135 | + |
| 136 | + private long writeToTarget( |
| 137 | + WritableByteChannel target, ByteArrayOutputStream byteWriter) |
| 138 | + throws IOException { |
| 139 | + |
| 140 | + int written = 0; |
| 141 | + synchronized(byteWriter) { |
| 142 | + while((target.isOpen()) && (written < byteWriter.size())) { |
| 143 | + ByteBuffer message = ByteBuffer.wrap(byteWriter.toByteArray()); |
| 144 | + written = target.write(message); |
| 145 | + if(written != byteWriter.size()) { |
| 146 | + System.out.println("Waiting..."); |
| 147 | + try { |
| 148 | + byteWriter.wait(1000); |
| 149 | + } catch (InterruptedException e) { |
| 150 | + // TODO Auto-generated catch block |
| 151 | + e.printStackTrace(); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return written; |
| 157 | + } |
| 158 | + |
| 159 | + private byte[] _boundary; |
| 160 | + private long _contentLength; |
| 161 | + private List<com.ning.http.client.Part> _parts; |
| 162 | + private List<RandomAccessFile> _files; |
| 163 | + private int _startPart; |
| 164 | + |
| 165 | +} |
0 commit comments