forked from chromiumembedded/java-cef
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCefPostDataElement.java
More file actions
128 lines (109 loc) · 3.51 KB
/
CefPostDataElement.java
File metadata and controls
128 lines (109 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
package org.cef.network;
/**
* Class used to represent a single element in the request post data. The
* methods of this class may be called on any thread.
*/
public abstract class CefPostDataElement {
/**
* Post data elements may represent either bytes or files.
*/
public static enum Type {
PDE_TYPE_EMPTY,
PDE_TYPE_BYTES,
PDE_TYPE_FILE,
PDF_TYPE_NUM_VALUES, // probably should be PDE_TYPE, but wrong in CEF too
}
// This CTOR can't be called directly. Call method create() instead.
CefPostDataElement() {}
@Override
protected void finalize() throws Throwable {
dispose();
super.finalize();
}
/**
* Create a new CefPostDataElement object.
*/
public static final CefPostDataElement create() {
return CefPostDataElement_N.createNative();
}
/**
* Removes the native reference from an unused object.
*/
public abstract void dispose();
/**
* Returns true if this object is read-only.
*/
public abstract boolean isReadOnly();
/**
* Remove all contents from the post data element.
*/
public abstract void setToEmpty();
/**
* The post data element will represent a file.
*/
public abstract void setToFile(String fileName);
/**
* The post data element will represent bytes. The bytes passed
* in will be copied.
*/
public abstract void setToBytes(int size, byte[] bytes);
/**
* Return the type of this post data element.
*/
public abstract Type getType();
/**
* Return the file name.
*/
public abstract String getFile();
/**
* Return the number of bytes.
*/
public abstract int getBytesCount();
/**
* Read up to size bytes into bytes and return the number of bytes
* actually read.
*/
public abstract int getBytes(int size, byte[] bytes);
@Override
public String toString() {
return toString(null);
}
public String toString(String mimeType) {
int bytesCnt = getBytesCount();
byte[] bytes = null;
if (bytesCnt > 0) {
bytes = new byte[bytesCnt];
}
boolean asText = false;
if (mimeType != null) {
if (mimeType.startsWith("text/"))
asText = true;
else if (mimeType.startsWith("application/xml"))
asText = true;
else if (mimeType.startsWith("application/xhtml"))
asText = true;
else if (mimeType.startsWith("application/x-www-form-urlencoded"))
asText = true;
}
String returnValue = "";
if (getType() == Type.PDE_TYPE_BYTES) {
int setBytes = getBytes(bytes.length, bytes);
returnValue += " Content-Length: " + bytesCnt + "\n";
if (asText) {
returnValue += "\n " + new String(bytes);
} else {
for (int i = 0; i < setBytes; i++) {
if (i % 40 == 0) returnValue += "\n ";
returnValue += String.format("%02X", bytes[i]) + " ";
}
}
returnValue += "\n";
} else if (getType() == Type.PDE_TYPE_FILE) {
returnValue += "\n Bytes of file: " + getFile() + "\n";
}
return returnValue;
}
}