Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

2025-05-29

JSON - enum as object instead of string

 

@JsonCreator was to address the issue of the json value as string, instead of enum object.

You're correct in your observation. The @JsonCreator annotation is indeed used to address the issue of deserializing JSON values into enum objects, not specifically for handling negative ids.
Given the context you've provided, it seems the issue might be related to how the enum values are being populated in the byId map.







2024-07-25

Java - Error: Unable to access jarfile

 


Error: Unable to access jarfile


changed
from java -jar abc.jar com.test.Main
to java -cp abc.jar com.test.Main













2024-07-22

Java SimpleDateTime pattern

 







yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2024-07-22T13:26:31.934+08:00




2024-06-21

Java Adoptium - JDK & JRE

 

https://github.com/microsoft/winget-pkgs/issues/67708

EclipseAdoptium.Temurin - Split into .jdk and .jre #67708
Closed
russellbanks opened this issue on Jul 27, 2022 · 10 comments


The EclipseAdoptium.Temurin packages are currently split as two separate packages: EclipseAdoptium.Temurin and EclipseAdoptium.TemurinJRE. I feel like it would be beneficial if, for clarity, the packages matched more like the ojdkbuild packages on here. (Shown below)









2024-05-14

iText write to outputstream




You should change the declaration of out to be of type ByteArrayOutputStream rather than just OutputStream. Then you can call ByteArrayOutputStream.toByteArray() to get the bytes, and construct a ByteArrayInputStream wrapping that.

As an aside, I wouldn't catch Exception like that, and I'd use a try-with-resources statement to close the document, assuming it implements AutoCloseable. It's also a good idea to follow Java naming conventions. So for example, you might have:

public InputStream createPdf() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();            
    try (Document doc = new Document(PageSize.A4, 50, 50, 50, 50)) {
        PdfWriter writer = PdfWriter.getInstance(doc, out);
        doc.open();
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
        table.addCell(cell);
        doc.add(table);
    }
    return new ByteArrayInputStream(out.toByteArray());
}






-----------------------------


/**
 * 
 */
package test.com.report.daily;

import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;



/**
 * @author Billson C
 *
 */
class TestByteArrayOf_iTextPdfToFile
{
private static final Logger _LOG = LoggerFactory.getLogger(TestByteArrayOf_iTextPdfToFile.class);

String pathRoot = "C:\\samples\\";

@BeforeAll
static void setUpBeforeClass() throws Exception {
}

@AfterEach
void tearDown() throws Exception {
}

String pathSeperator = "/";
DateFormat _DTF = new SimpleDateFormat("HHmm");

@Test
void test() throws IOException {

String currentPathFolder = null;
String currentPathFile = null;
String currentTimingGenerated = _DTF.format(Calendar.getInstance().getTime());

currentPathFolder = StringUtils.join(pathRoot, pathSeperator, currentTimingGenerated, pathSeperator
// , currentPortLocationCode
);

Files.createDirectories(Paths.get(currentPathFolder));

String dash = "-";

String filename = StringUtils.join("bytearray-of-itext-pdf-to-file", ".", "pdf");

String content = StringUtils.join("The filename of this file is: ", filename);

currentPathFile = StringUtils.join(currentPathFolder, pathSeperator, filename);

File fPdf = new File(currentPathFile);

try (FileOutputStream fosPdf = new FileOutputStream(fPdf);) {

byte[] aryByte = _getAryByteOfPdf(content);

fosPdf.write(aryByte);

// outputStream.writeTo(fosPdf);

} catch (Exception e) {
_LOG.error("Error while test|{}", e.getMessage(), e);
}

assertTrue(fPdf.exists());
// TO DO: suppose to check file content equal to filename

String contentFromPdf = PdfTextExtractor.getTextFromPage(new PdfDocument(new PdfReader(fPdf)).getFirstPage());

_LOG.debug("contentFromPdf|{}", contentFromPdf);

assertEquals(content, contentFromPdf);

}

/**
* @param content
*/
protected byte[] _getAryByteOfPdf(String content) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(outputStream);
// PdfWriter pdfWriter = new PdfWriter(currentPathFile);
Document document = new Document(new PdfDocument(pdfWriter
// new PdfWriter("./hello-pdf.pdf")
), PageSize.A4.rotate())) {

document.add(new Paragraph(new Text(content)));
document.close();

return outputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

}









Google Referrals