java 代码求解

stephen2017
  • 1
新手上路,请多包涵
package com.bytedance.retrofit2.mime;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

public final class MultipartTypedOutput implements TypedOutput {
    final class MimePart {
        private final TypedOutput body;
        private final String boundary;
        private boolean isBuilt;
        private final boolean isFirst;
        private final String name;
        private byte[] partBoundary;
        private byte[] partHeader;
        private final String transferEncoding;

        public MimePart(String arg1, String arg2, TypedOutput arg3, String arg4, boolean arg5) {
            super();
            this.name = arg1;
            this.transferEncoding = arg2;
            this.body = arg3;
            this.isFirst = arg5;
            this.boundary = arg4;
        }

        private void build() {
            if(this.isBuilt) {
                return;
            }

            this.partBoundary = MultipartTypedOutput.buildBoundary(this.boundary, this.isFirst, false);
            this.partHeader = MultipartTypedOutput.buildHeader(this.name, this.transferEncoding, this.body);
            this.isBuilt = true;
        }

        public final long size() {
            this.build();
            long v2 = -1;
            if(this.body.length() > v2) {
                return this.body.length() + (((long)this.partBoundary.length)) + (((long)this.partHeader.length));
            }

            return v2;
        }

        public final void writeTo(OutputStream arg2) throws IOException {
            this.build();
            arg2.write(this.partBoundary);
            arg2.write(this.partHeader);
            this.body.writeTo(arg2);
        }
    }

    private final String boundary;
    private final byte[] footer;
    private long length;
    private final List mimeParts;

    public MultipartTypedOutput() {
        this(UUID.randomUUID().toString());
    }

    MultipartTypedOutput(String arg3) {
        super();
        this.mimeParts = new LinkedList();
        this.boundary = arg3;
        this.footer = MultipartTypedOutput.buildBoundary(arg3, false, true);
        this.length = ((long)this.footer.length);
    }

    public final void addPart(String arg2, TypedOutput arg3) {
        this.addPart(arg2, "binary", arg3);
    }

    public final void addPart(String arg8, String arg9, TypedOutput arg10) {
        if(arg8 != null) {
            if(arg9 != null) {
                if(arg10 != null) {
                    MimePart v6 = new MimePart(arg8, arg9, arg10, this.boundary, this.mimeParts.isEmpty());
                    this.mimeParts.add(v6);
                    long v8 = v6.size();
                    long v0 = -1;
                    if(v8 == v0) {
                        this.length = v0;
                        return;
                    }

                    long v2 = this.length;
                    if(v2 != v0) {
                        this.length = v2 + v8;
                    }

                    return;
                }

                throw new NullPointerException("Part body must not be null.");
            }

            throw new NullPointerException("Transfer encoding must not be null.");
        }

        throw new NullPointerException("Part name must not be null.");
    }

    public static byte[] buildBoundary(String arg3, boolean arg4, boolean arg5) {
        StringBuilder v1;
        String v0 = "--";
        try {
            v1 = new StringBuilder(arg3.length() + 8);
        }
        catch(IOException v3) {
            goto label_21;
        }

        String v2 = "\r\n";
        if(!arg4) {
            try {
                v1.append(v2);
            label_8:
                v1.append(v0);
                v1.append(arg3);
                if(arg5) {
                    v1.append(v0);
                }

                v1.append(v2);
                return v1.toString().getBytes("UTF-8");
            }
            catch(IOException v3) {
            label_21:
                throw new RuntimeException("Unable to write multipart boundary", ((Throwable)v3));
            }
        }

        goto label_8;
    }

    public static byte[] buildHeader(String arg5, String arg6, TypedOutput arg7) {
        try {
            StringBuilder v0 = new StringBuilder(0x80);
            v0.append("Content-Disposition: form-data; name=\"");
            v0.append(arg5);
            arg5 = arg7.fileName();
            if(arg5 != null) {
                v0.append("\"; filename=\"");
                v0.append(arg5);
            }

            v0.append("\"\r\nContent-Type: ");
            v0.append(arg7.mimeType());
            long v1 = arg7.length();
            if(v1 != -1) {
                v0.append("\r\nContent-Length: ");
                v0.append(v1);
            }

            v0.append("\r\nContent-Transfer-Encoding: ");
            v0.append(arg6);
            v0.append("\r\n\r\n");
            return v0.toString().getBytes("UTF-8");
        }
        catch(IOException v5) {
            throw new RuntimeException("Unable to write multipart header", ((Throwable)v5));
        }
    }

    public final String fileName() {
        return null;
    }

    public final int getPartCount() {
        return this.mimeParts.size();
    }

    final List getParts() throws IOException {
        ArrayList v0 = new ArrayList(this.mimeParts.size());
        Iterator v1 = this.mimeParts.iterator();
        while(v1.hasNext()) {
            Object v2 = v1.next();
            ByteArrayOutputStream v3 = new ByteArrayOutputStream();
            ((MimePart)v2).writeTo(((OutputStream)v3));
            ((List)v0).add(v3.toByteArray());
        }

        return ((List)v0);
    }

    public final long length() {
        return this.length;
    }

    public final String md5Stub() {
        ByteArrayInputStream v1;
        ByteArrayOutputStream v0;
        String v2 = null;
        if(this.length == -1) {
            return v2;
        }

        try {
            v0 = new ByteArrayOutputStream();
        }
        catch(Throwable ) {
            v0 = ((ByteArrayOutputStream)v2);
            v1 = ((ByteArrayInputStream)v0);
            goto label_20;
        }

        try {
            this.writeTo(((OutputStream)v0));
            v1 = new ByteArrayInputStream(v0.toByteArray());
        }
        catch(Throwable ) {
            v1 = ((ByteArrayInputStream)v2);
            goto label_20;
        }

        try {
            v2 = DigestUtil.md5Hex(((InputStream)v1));
            goto label_12;
        }
        catch(Throwable ) {
        }

    label_20:
        if(v0 != null) {
            try {
                v0.close();
                goto label_23;
            }
            catch(Throwable ) {
            label_23:
                if(v1 != null) {
                    try {
                        v1.close();
                        return v2;
                    }
                    catch(Throwable ) {
                        return v2;
                    }
                }

                return v2;
            }
        }

        goto label_23;
        try {
        label_12:
            v0.close();
            goto label_13;
        }
        catch(Throwable ) {
            try {
            label_13:
                v1.close();
                return v2;
            }
            catch(Throwable ) {
                return v2;
            }
        }
    }

    public final String mimeType() {
        StringBuilder v0 = new StringBuilder("multipart/form-data; boundary=");
        v0.append(this.boundary);
        return v0.toString();
    }

    public final void writeTo(OutputStream arg3) throws IOException {
        Iterator v0 = this.mimeParts.iterator();
        while(v0.hasNext()) {
            v0.next().writeTo(arg3);
        }

        arg3.write(this.footer);
    }
}

这里的

  public final String md5Stub() {
        ByteArrayInputStream v1;
        ByteArrayOutputStream v0;
        String v2 = null;
        if(this.length == -1) {
            return v2;
        }

        try {
            v0 = new ByteArrayOutputStream();
        }
        catch(Throwable ) {
            v0 = ((ByteArrayOutputStream)v2);
            v1 = ((ByteArrayInputStream)v0);
            goto label_20;
        }

        try {
            this.writeTo(((OutputStream)v0));
            v1 = new ByteArrayInputStream(v0.toByteArray());
        }
        catch(Throwable ) {
            v1 = ((ByteArrayInputStream)v2);
            goto label_20;
        }

这里的this.writeTo(((OutputStream)v0))
是把一个空的v0 输出流写到文件? 这个意义是啥呢?这么长的代码就是写一个空的字节流?

回复
阅读 612
1 个回答

看到这烂代码用goto就已经没有读下去的欲望了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏