1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-14 02:06:25 +00:00

Merge remote-tracking branch 'origin/master'

This commit is contained in:
Benjamin Gamard
2017-12-11 10:17:19 +01:00
4 changed files with 34 additions and 86 deletions

View File

@@ -1,14 +1,8 @@
package com.sismics.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* HTTP request utilities.
@@ -17,77 +11,17 @@ import java.net.URLConnection;
*/
public class HttpUtil {
/**
* Logger.
* Format of the expires header.
*/
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
private static final SimpleDateFormat EXPIRES_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
/**
* Loads the content of an URL into a string.
*
* @param url URL to load
* @return Contents of the resource
* Build an Expires HTTP header.
*
* @param futureTime Expire interval
* @return Formatted header value
*/
public static String readUrlIntoString(URL url) {
URLConnection connection;
BufferedReader in = null;
try {
connection = url.openConnection();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
return sb.toString();
} catch (IOException e) {
if (log.isErrorEnabled()) {
log.error("Error reading URL", e);
}
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
// NOP
}
}
}
}
public static String postUrl(URL url, String data) throws IOException {
OutputStreamWriter wr = null;
BufferedReader rd = null;
try {
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = rd.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} finally {
if (wr != null) {
try {
wr.close();
} catch (IOException e) {
// NOP
}
}
if (rd != null) {
try {
rd.close();
} catch (IOException e) {
// NOP
}
}
}
public static String buildExpiresHeader(long futureTime) {
return EXPIRES_FORMAT.format(new Date().getTime() + futureTime);
}
}

View File

@@ -9,6 +9,7 @@ import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
@@ -40,6 +41,17 @@ public class ImageUtil {
iwp.setCompressionQuality(1.f);
imageOutputStream = ImageIO.createImageOutputStream(outputStream);
writer.setOutput(imageOutputStream);
if (image.getColorModel().hasAlpha()) {
// Strip alpha channel
BufferedImage noAlphaImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics graphics = noAlphaImage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
graphics.drawImage(image, 0, 0, null);
image = noAlphaImage;
}
IIOImage iioImage = new IIOImage(image, null, null);
writer.write(null, iioImage, iwp);
} finally {