use of com.codename1.rad.models.ContentType in project CodenameOne by codenameone.
the class JavaSEPort method openInputStream.
/**
* @inheritDoc
*/
public InputStream openInputStream(Object connection) throws IOException {
if (connection instanceof String) {
FileInputStream fc = new FileInputStream(unfile((String) connection));
BufferedInputStream o = new BufferedInputStream(fc, (String) connection);
return o;
}
if (netMonitor != null || slowConnectionMode || disconnectedMode) {
final NetworkRequestObject nr = getByConnection((URLConnection) connection);
if (nr != null || slowConnectionMode || disconnectedMode) {
if (slowConnectionMode) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
if (nr != null) {
nr.setTimeServerResponse(System.currentTimeMillis());
}
if (disconnectedMode) {
throw new IOException("Unreachable");
}
HttpURLConnection con = (HttpURLConnection) connection;
String headers = "";
Map<String, List<String>> map = con.getHeaderFields();
for (String header : map.keySet()) {
headers += header + "=" + map.get(header) + "\n";
}
if (nr != null) {
nr.setResponseHeaders(headers);
nr.setResponseBody("");
}
InputStream is;
if (con.getResponseCode() >= 200 && con.getResponseCode() < 300) {
is = con.getInputStream();
} else {
is = con.getErrorStream();
}
boolean isText = false;
String contentType = con.getContentType();
if (contentType != null) {
if (contentType.startsWith("text/") || contentType.contains("json") || contentType.contains("css") || contentType.contains("javascript")) {
isText = true;
}
}
final boolean fIsText = isText;
InputStream i = new BufferedInputStream(is) {
public synchronized int read(byte[] b, int off, int len) throws IOException {
int s = super.read(b, off, len);
if (nr != null) {
if (fIsText && s > -1) {
nr.setResponseBody(nr.getResponseBody() + new String(b, off, len));
}
}
if (slowConnectionMode) {
try {
Thread.sleep(len);
} catch (Exception e) {
}
}
if (disconnectedMode) {
throw new IOException("Unreachable");
}
return s;
}
@Override
public void close() throws IOException {
super.close();
if (nr != null) {
nr.setTimeComplete(System.currentTimeMillis());
}
}
};
return i;
}
}
if (connection instanceof HttpURLConnection) {
HttpURLConnection ht = (HttpURLConnection) connection;
if (ht.getResponseCode() < 400) {
return new BufferedInputStream(ht.getInputStream());
}
return new BufferedInputStream(ht.getErrorStream());
} else {
return new BufferedInputStream(((URLConnection) connection).getInputStream());
}
}
use of com.codename1.rad.models.ContentType in project CodeRAD by shannah.
the class DefaultTableCellEditor method getTableCellEditorComponent.
@Override
public Component getTableCellEditorComponent(Table table, Object value, boolean isSelected, int row, int column) {
TextField tf = new TextField();
TableModel model = table.getModel();
ContentType contentType = model.getCellContentType(row, column);
String strVal = value == null ? "" : String.valueOf(value);
tf.setText(strVal);
tf.addDataChangedListener((type, index) -> {
table.getModel().setValueAt(ContentType.convert(Text, tf.getText(), contentType), row, column);
});
return tf;
}
Aggregations