Java FTP using Apache commons throws "IOException caught while copying" - java

I have made a JavaFX application which includes uploading large files (> 1GB) to a server. Every time I got the same error on same place. Any Suggestions what am I doing wrong here.
ftpclient.connect(server, port);
ftpclient.login(ftpuser, ftppass);
ftpclient.enterLocalPassiveMode();
ftpclient.setKeepAlive(true);
ftpclient.setControlKeepAliveTimeout(3000);
Task<Void> copyMnt = new Task<Void>() {
#Override
protected Void call(){
try {
new Thread(new FTPHandler(ftpclient, source , dest)).run();
} catch (IOException ex) {
Logger.getLogger(MyAppController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
};
new Thread(copyMnt).start();
Now on the FTPHandler Class
// The constructor will set the ftpclient, source and destinations.
#Override
public void run() {
try {
uploadDirectory(this.getClient(), this.getDest(), this.getSrc(), "");
} catch (IOException ex) {
Logger.getLogger(FTPHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void uploadDirectory(FTPClient ftpClient,
String remoteDirPath, String localParentDir, String remoteParentDir)
throws IOException {
File localDir = new File(localParentDir);
File[] subFiles = localDir.listFiles();
if (subFiles != null && subFiles.length > 0) {
for (File item : subFiles) {
String remoteFilePath = remoteDirPath + "/" + remoteParentDir
+ "/" + item.getName();
if (remoteParentDir.equals("")) {
remoteFilePath = remoteDirPath + "/" + item.getName();
}
if (item.isFile()) {
// upload the file
String localFilePath = item.getAbsolutePath();
java.util.Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()) + " : Uploading :: " + localFilePath + " to " + remoteFilePath);
boolean uploaded = uploadSingleFile(ftpClient,
localFilePath, remoteFilePath);
if (uploaded) {
System.out.println("Success : "
+ remoteFilePath);
} else {
System.out.println("Failed : "
+ localFilePath);
}
} else {
// create directory on the server
boolean created = ftpClient.makeDirectory(remoteFilePath);
if (created) {
System.out.println("CREATED the directory: "
+ remoteFilePath);
} else {
System.out.println("COULD NOT create the directory: "
+ remoteFilePath);
}
// upload the sub directory
String parent = remoteParentDir + "/" + item.getName();
if (remoteParentDir.equals("")) {
parent = item.getName();
}
localParentDir = item.getAbsolutePath();
uploadDirectory(ftpClient, remoteDirPath, localParentDir,
parent);
}
}
}
}
Every time I am uploading the files (files are of different types like .iso, .dat etc), The first few files (upload sequence is like first few hundred files are smaller i.e less than few MBs then Last 10 files are more than 1 GB big)will be successfully uploaded (i.e all smaller files and 2 of the last 10 files) but when it starts uploading the third big file I get following exception.
SEVERE: null
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
at org.apache.commons.net.io.Util.copyStream(Util.java:134)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)

The CopyStreamException has a "cause" exception. Check that using the .getCause(), to see what went wrong.
See the Util.copyStream method:
public static final long copyStream(InputStream source, OutputStream dest,
int bufferSize, long streamSize,
CopyStreamListener listener,
boolean flush)
throws CopyStreamException
{
int bytes;
long total = 0;
byte[] buffer = new byte[bufferSize >= 0 ? bufferSize : DEFAULT_COPY_BUFFER_SIZE];
try
{
while ((bytes = source.read(buffer)) != -1)
{
....
}
}
catch (IOException e)
{
throw new CopyStreamException("IOException caught while copying.",
total, e);
}
return total;
}
Somewhere in your uploadSingleFile function, do
try
{
ftpClient.storeFile(...)
}
catch (Exception e)
{
e.printStackTrace();
if (e.getCause() != null)
{
e.getCause().printStackTrace();
}
}
I do not know Java, so the code may not be 100% correct.
See also Getting full string stack trace including inner exception.

Related

delete image from the folder

I want to delete one image from the folder when I delete a user from the table with this image. Here is my code:
//first I can the function that finds a path of the image in the folder
public void deleteStudent(String name) {
try {
CallableStatement statement = null;
Connection data = getmyConnection();
statement = data.prepareCall("{call delete_student(?)}");
statement.setString(1, name);
//statement.registerOutParameter(2, java.sql.Types.VARCHAR);
statement.executeQuery();
} catch (Exception c) {
c.printStackTrace();
}
//After I call the function to delete image from directory
deleteImageDerictory(name);
}
This method allows choosing the image from the directory when I get the image I add the path in jTextField1.getText().
//use this method to get the path of my image.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpeg", "jpg", "png");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if(result ==JFileChooser.APPROVE_OPTION) {
File selectedFile = file.getSelectedFile();
//GET ABSOLUTE PATH OF PICTURES
jTextField1.setText(selectedFile.getAbsolutePath());
//addPicture.setText(selectedFile.getName());
//GET NAME OF PICTURES
//getPicName = selectedFile.getName();
} else if(result == JFileChooser.CANCEL_OPTION) {
System.out.println("File not found!");
}
}
//I use this method to call another method deleteImageDerictory(jTextField1.getText());
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
deleteImageDerictory(jTextField1.getText());
}catch(Exception e) {
e.printStackTrace();
}
}
public void deleteImageDerictory(String name) {
String pictureName = null;
try {
CallableStatement statement = null;
Connection data = getmyConnection();
statement = data.prepareCall("{call get_picture(?)}");
statement.setString(1, name);
//statement.registerOutParameter(2, java.sql.Types.VARCHAR);
myResults = statement.executeQuery();
while (myResults.next()) {
//COPY PATH IN pictureName
pictureName = myResults.getString(1);
}
myResults.close();
} catch (Exception c) {
c.printStackTrace();
}
//DELETE ELEMENT FROM FOLDER
File sourceFile = new File(pictureName);
File file = new File("/Computer/NetBeans IDE 8.2/NewDataBase/src/newdatabase/images/");
images = file.listFiles();
File file2 = new File(file.getAbsolutePath(), sourceFile.getName() );
boolean deleted = file2.delete();
System.out.println(deleted);
}
I just don't know how to delete image from folder when I find it. Any ideas?
You can use the modern and more powerful java.nio.* instead of the old fashioned java.io.File. You just have to create a Path object containing the path to the folder where the images are stored and resolve the file name:
//DELETE ELEMENT FROM FOLDER
Path imagesPath = Paths.get(
"/Computer/NetBeans IDE 8.2/NewDataBase/src/newdatabase/images/" +
pictureName);
try {
Files.delete(imagesPath);
System.out.println("File "
+ imagesPath.toAbsolutePath().toString()
+ " successfully removed");
} catch (IOException e) {
System.err.println("Unable to delete "
+ imagesPath.toAbsolutePath().toString()
+ " due to...");
e.printStackTrace();
}
EDIT due to discussion in comments below:
This is a very simple approach that deletes a file chosen via JFileChooser:
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int result = jfc.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println("Chosen file: " +
selectedFile.getAbsolutePath() +
" will be deleted!");
Path pathToBeDeleted = Paths.get(selectedFile.getAbsolutePath());
try {
Files.delete(pathToBeDeleted);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have just tried it myself and it successfully removes the chosen file.
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView()./0());
int result = jfc.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println("Chosen file: " +
selectedFile.getAbsolutePath() +
" will be deleted!");
Path data= Paths.get(selectedFile.getAbsolutePath());
try {
Files.delete(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Reading huge amount of data 50Million from database and writing it to Fixed width and BSON Multithreading slower than serial writing

I have been trying to read 50 Million records from DB2 as a compressed stream and would then like to create 2 jar files one of them in fixed width format and the other in the BSON format.
I have been able to make the code work but since the time taken to pull the code and write it to these files takes close to 120 minutes I redesigned the code to use a producer consumer model. I how ever see performance being degraded using this model. Multi-threading for some reason is not working as expected.
Producer producer = new Producer(queue1, queue2, url, user, pass, driver, strQuery);
Consumer1 consumer1 = new Consumer1(queue1, outputDatFile, fileDat.getName());
Consumer2 consumer2 = new Consumer2(queue2, outputDatBSONFile, fileBSONDat.getName());
ExecutorService threadPool = Executors.newFixedThreadPool(3);
Future producerStatus = threadPool.submit(producer);
threadPool.execute(consumer1);
threadPool.execute(consumer2);
try {
System.out.println("This will wait for the producer to wait " + producerStatus.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
threadPool.shutdown();
long end = System.currentTimeMillis();
System.out.println("End Time: " + end);
long elapsedTimeMillis = end - start;
float elapsedTimeSec = (float) elapsedTimeMillis / 1000.0F;
System.out.println("Total Time: " + elapsedTimeSec + " seconds..");
if (!(errorStatus)) {
System.out.println("Successful exit...");
System.exit(0);
} else {
System.out.println("Exiting - Fatal Errors Encountered!");
System.exit(1);
}
}
private static JarOutputStream getJar(String outputDatFile, String fileName)
throws FileNotFoundException, IOException {
JarOutputStream jarOutPutStream = new JarOutputStream(
new BufferedOutputStream(new FileOutputStream(new File(outputDatFile + ".jar"))));
jarOutPutStream.setMethod(JarOutputStream.DEFLATED);
JarEntry ze = new JarEntry(fileName);
jarOutPutStream.putNextEntry(ze);
return jarOutPutStream;
}
public class Consumer1 implements Runnable {
private BlockingQueue<String> queue;
private String outputDatFile;
private String datFileName;
public Consumer1(BlockingQueue<String> queue, String outputDatFile, String datFileName) {
this.queue = queue;
this.outputDatFile = outputDatFile;
this.datFileName = datFileName;
}
#Override
public void run() {
JarOutputStream jarOutPutStreamText = null;
try {
jarOutPutStreamText = getJar(outputDatFile, datFileName);
int recordsWritten = 0;
while (true) {
recordsWritten++;
try {
String objectRetrieved = queue.take();
jarOutPutStreamText.write(objectRetrieved.getBytes());
jarOutPutStreamText.flush();
if (recordsWritten % 100000 == 0) {
System.out.println("Written Records Count Queue 1 " + recordsWritten);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (jarOutPutStreamText != null) {
try {
jarOutPutStreamText.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class Consumer2 implements Runnable {
private BlockingQueue<DBObject> queue2;
private String outputDatBSONFile;
private String bsonFileName;
public Consumer2(BlockingQueue<DBObject> queue2, String outputDatBSONFile, String bsonFileName) {
this.queue2 = queue2;
this.outputDatBSONFile = outputDatBSONFile;
this.bsonFileName = bsonFileName;
}
#Override
public void run() {
JarOutputStream jarOutPutStreamBSON = null;
try {
jarOutPutStreamBSON = getJar(outputDatBSONFile, bsonFileName);
BSONFileWriter bsonWriter = new BSONFileWriter(jarOutPutStreamBSON);
int recordsWritten = 0;
while (true) {
recordsWritten++;
try {
DBObject objectRetrieved = queue2.take();
bsonWriter.write(objectRetrieved);
bsonWriter.flush();
if (recordsWritten % 100000 == 0) {
System.out.println("Written Records Count Queue 2 " + recordsWritten);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (jarOutPutStreamBSON != null) {
try {
jarOutPutStreamBSON.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class Producer implements Runnable {
private BlockingQueue<String> queue1;
private BlockingQueue<DBObject> queue2;
private String url;
private String user;
private String pass;
private String driver;
private String strQuery;
public Producer(BlockingQueue<String> queue1, BlockingQueue<DBObject> queue2, String url, String user,
String pass, String driver, String strQuery) {
this.queue1 = queue1;
this.queue2 = queue2;
this.url = url;
this.pass = pass;
this.driver = driver;
this.strQuery = strQuery;
}
#Override
public void run() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
con.setAutoCommit(false);
Map<String, Object> mapper = new HashMap<String, Object>();
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(20000);
System.out.println(
"Attempting to execute statement: " + System.getProperty("line.separator") + strQuery);
strQuery = strQuery.replace(System.getProperty("line.separator"), " ");
rs = st.executeQuery(strQuery);
ResultSetMetaData md = rs.getMetaData();
try {
int col = md.getColumnCount();
int resultSetCounter = 0;
while (rs.next()) {
String strQueryOutput = "";
resultSetCounter++;
for (int x = 1; x <= col; ++x) {
String outPut = "";
if (md.getColumnTypeName(x).equals("DECIMAL")) {
StringAlignUtils util = new StringAlignUtils(md.getColumnDisplaySize(x),
Alignment.RIGHT);
outPut = util.format(rs.getString(x));
strQueryOutput = strQueryOutput + outPut;
mapper.put(md.getColumnName(x), outPut);
} else if (md.getColumnTypeName(x).equals("NUMERIC")) {
StringAlignUtils util = new StringAlignUtils(md.getColumnDisplaySize(x),
Alignment.RIGHT);
outPut = util.format(rs.getString(x));
strQueryOutput = strQueryOutput + outPut;
mapper.put(md.getColumnName(x), outPut);
} else if (md.getColumnTypeName(x).equals("CHAR() FOR BIT DATA")) {
char charData = rs.getString(x).charAt(0);
outPut = charData + "";
StringAlignUtils util = new StringAlignUtils(md.getColumnDisplaySize(x),
Alignment.RIGHT);
outPut = util.format(outPut);
strQueryOutput = strQueryOutput + outPut;
mapper.put(md.getColumnName(x), outPut);
} else {
StringAlignUtils util = new StringAlignUtils(md.getColumnDisplaySize(x),
Alignment.RIGHT);
outPut = util.format(rs.getString(x));
strQueryOutput = strQueryOutput + outPut;
mapper.put(md.getColumnName(x), outPut);
}
}
if (resultSetCounter % 100000 == 0) {
System.out.println(" The counter is " + resultSetCounter);
}
strQueryOutput = strQueryOutput + '\n';
queue1.put(strQueryOutput);
queue2.put(new BasicDBObject(mapper));
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error: " + e.getMessage());
System.err.println("Exiting!");
System.exit(1);
}
System.out.println("Query results successfully returned...");
} catch (SQLException s) {
System.err.println("SQL statement is not executed!");
System.err.println("Error: " + s.getMessage());
} finally {
System.out.println("Trying to Close ResultSet and Statement...");
if (rs != null) {
System.out.println("Closing ResultSet..");
rs.close();
}
if (st != null) {
System.out.println("Closing Statement..");
st.close();
}
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
System.out.println("Trying to Close database connection..");
if (con != null) {
System.out.println("Closing database connection..");
con.close();
}
} catch (SQLException exception) {
exception.printStackTrace();
}
}
}
}
I think I got the reason why the file was getting generated in incorrect format.
There are 2 problems
1) Complete data was not being written.
2) Stream was not being closed.
The problem is fixed but even after having 1 Producer and 2 consumers the time taken to process is upwards of 90minutes for 50 Million records can some one point me the areas that might make this program faster.
There are sufficient elements that slow things down.
One small error
String objectRetrieved = queue.take();
jarOutPutStreamText.write(objectRetrieved.getBytes());
Maybe for the last else left aligned?
Should specify the encoding
jarOutPutStreamText.write(objectRetrieved.getBytes(StandardCharsets.UTF_8));
The easiest to spot:
String strQueryOutput = "";
should be
StringBuilder strQueryOutput = new StringBuilder(1000 /* output size */);
The DB2 SQL may benefit by a last line WITH ur (uncommited read).
The StringAlignUtils should be created once, outside the loop.
In fact leaving it to the database to format might be the fastest solution.
If you need to pass a map for every record, you can prepare a map, and hold an array of Map.Entry entries by column index (minus 1). To immediately alter the value by column index.
A smaller fetch size (?) and more java heap might help to.
My experience in this field concern mainly GzippedOutputStream (which I would have expected here too), where the fastest compression is not the highest compression.
Made a few changes as suggested above that helped, I have how ever noticed that writing to a file with out zipping is faster than writing to a zipped stream. Writing to BufferedWriter takes approximately 14 minutes to process 1 million records and 22.8 minutes to write to a zipped stream.

Heap memory gets full and throw : java.lang.OutOfMemoryError: Java Heap Space [duplicate]

This question already has answers here:
java.lang.OutOfMemoryError: Java heap space
(12 answers)
Closed 5 years ago.
The following code of servlet receives huge JSON string every minute and near about after 2 hours I always got the OutOfMemoryError: Java Heap Space
public class GetScanAlertServlet extends HttpServlet {
private String scanType = "";
private static final String path = "D:\\Mobile_scan_alerts8180";
private static final String stockFileName = "stock.txt";
private static final String foFileName = "fo.txt";
private static Logger logger = null;
private String currDate = "";
private DateFormat dateFormat;
private StringBuffer stockData;
private StringBuffer foData;
StringBuffer data = new StringBuffer("");
// For average time of received data
private static float sum = 0;
private static float count = 0;
private static float s_sum = 0;
private static float s_count = 0;
private static float fo_sum = 0;
private static float fo_count = 0;
private static final File dir = new File(path);
private static final File stockFile = new File(path + "\\" + stockFileName);
private static final File foFile = new File(path + "\\" + foFileName);
public void init() {
logger = MyLogger.getScanAlertLogger();
if(logger == null) {
MyLogger.createLog();
logger = MyLogger.getScanAlertLogger();
}
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
String strScan = "";
try {
String asof = null;
scanType = request.getParameter("type");
scanType = scanType == null ? "" : scanType;
if(scanType.length() > 0){
if(scanType.equalsIgnoreCase("s")) {
stockData = null;
stockData = new StringBuffer(request.getParameter("scanData"));
stockData = stockData == null ? new StringBuffer("") : stockData;
} else {
foData = null;
foData = new StringBuffer(request.getParameter("scanData"));
foData = foData == null ? new StringBuffer("") : foData;
}
}
asof = request.getParameter("asof");
asof = asof == null ? "" : asof.trim();
// Date format without seconds
DateFormat formatWithoutSec = new SimpleDateFormat("yyyy/MM/dd HH:mm");
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date tmp = new Date();
// format: yyyy/MM/dd HH:mm:ss
currDate = dateFormat.format(tmp);
//format: yyyy/MM/dd HH:mm
Date asofDate = formatWithoutSec.parse(asof);
Date cDate = formatWithoutSec.parse(currDate);
cDate.setSeconds(0);
System.out.println(asofDate.toString()+" || "+cDate.toString());
int isDataExpired = asofDate.toString().compareTo(cDate.toString());
if(isDataExpired > 0 || isDataExpired == 0) {
if(scanType != null && scanType.length() > 0) {
checkAndCreateDir();
strScan = scanType.equalsIgnoreCase("s") ? "Stock Data Recieved at "+currDate
: "FO Data Recieved at "+currDate;
//System.out.println(strScan);
} else {
strScan = "JSON of scan data not received properly at "+currDate;
//System.out.println("GSAS: received null or empty");
}
} else {
strScan = "GSAS: " + scanType + ": Received Expired Data of "+asofDate.toString()+" at "+cDate.toString();
System.out.println(strScan);
}
scanType = null;
} catch (Exception ex) {
strScan = "Mobile server issue for receiving scan data";
System.out.println("GSAS: Exception-1: "+ex);
logger.error("GetScanAlertServlet: processRequest(): Exception: "+ex.toString());
} finally {
logger.info("GetScanAlertServlet: "+strScan);
out.println(strScan);
}
}
private void checkAndCreateDir() {
try {
boolean isStock = false;
Date ddate = new Date();
currDate = dateFormat.format(ddate);
sum += ddate.getSeconds();
count++;
logger.info("Total Average Time: "+(sum/count));
if(scanType.equalsIgnoreCase("s")){ //For Stock
setStockData(stockData);
Date date1 = new Date();
currDate = dateFormat.format(date1);
s_sum += date1.getSeconds();
s_count++;
logger.info("Stock Average Time: "+(s_sum/s_count));
//file = new File(path + "\\" + stockFileName);
isStock = true;
} else if (scanType.equalsIgnoreCase("fo")) { //For FO
setFOData(foData);
Date date2 = new Date();
currDate = dateFormat.format(date2);
fo_sum += date2.getSeconds();
fo_count++;
logger.info("FO Average Time: "+(fo_sum/fo_count));
//file = new File(path + "\\" +foFileName);
isStock = false;
}
if(!dir.exists()) { // Directory not exists
if(dir.mkdir()) {
if(isStock)
checkAndCreateFile(stockFile);
else
checkAndCreateFile(foFile);
}
} else { // Directory already exists
if(isStock)
checkAndCreateFile(stockFile);
else
checkAndCreateFile(foFile);
}
} catch (Exception e) {
System.out.println("GSAS: Exception-2: "+e);
logger.error("GetScanAlertServlet: checkAndCreateDir(): Exception: "+e);
}
}
private void checkAndCreateFile(File file) {
try{
if(!file.exists()){ // File not exists
if(file.createNewFile()){
writeToFile(file);
}
} else { // File already exists
writeToFile(file);
}
} catch (Exception e) {
System.out.println("GSAS: Exception-3: "+e);
logger.error("GetScanAlertServlet: checkAndCreateFile(): Exception: "+e.toString());
}
}
private void writeToFile(File file) {
FileOutputStream fop = null;
try{
if(scanType.equalsIgnoreCase("s")){ //For Stock
data = getStockData();
} else if (scanType.equalsIgnoreCase("fo")) { //For FO
data = getFOData();
}
if(data != null && data.length() > 0 && file != null){
fop = new FileOutputStream(file);
byte[] contentBytes = data.toString().getBytes();
for(byte b : contentBytes){
fop.write(b);
}
//fop.write(contentBytes);
fop.flush();
} else {
System.out.println("GSAS: Data is null/empty string");
logger.info("GSAS: Data is null or empty string");
}
data = null;
} catch (Exception e) {
System.out.println("GSAS: Exception-4: "+e);
logger.info("GetScanAlertServlet: writeToFile(): Exception: "+e.toString());
} finally {
try {
if(fop != null)
fop.close();
} catch (IOException ex) {
java.util.logging.Logger.getLogger(GetScanAlertServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private String readFromFile(String fileName){
String fileContent = "";
try{
String temp = "";
File file = new File(fileName);
if(file.exists()){
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((temp = br.readLine()) != null)
{
fileContent += temp;
}
br.close();
} else {
System.out.println("GSAS: File not exists to read");
logger.info("GetScanAlertServlet: File not exists to read");
}
temp = null;
file = null;
} catch (Exception e) {
System.out.println("GSAS: Exception-5: "+e);
logger.error("GetScanAlertServlet: readFromFile(): Exception: "+e.toString());
}
return fileContent;
}
public StringBuffer getStockData() {
//String temp="";
//StringBuffer temp = (StringBuffer)scanDataSession.getAttribute("stock");
//if(temp != null && temp.length() > 0) {
// return temp;
//}
if(stockData != null && stockData.length() > 0){
return stockData;
} else {
stockData = null;
stockData = new StringBuffer(readFromFile(path + "\\"+ stockFileName));
return stockData;
}
}
public StringBuffer getFOData(){
//String temp="";
//StringBuffer temp = (StringBuffer)scanDataSession.getAttribute("fo");
//if(temp != null && temp.length() > 0) {
// return temp;
//}
if(foData != null && foData.length() > 0) {
return foData;
} else {
foData = null;
foData = new StringBuffer(readFromFile(path + "\\" + foFileName));
return foData;
}
}
}
I always get the following exception after every 2 hours when I restart my jboss server and as solution I've also increased Heap size but same problem still exists
ERROR [[GetScanAlertServlet]] Servlet.service() for servlet GetScan
AlertServlet threw exception
java.lang.OutOfMemoryError: Java heap space
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at GetScanAlertServlet.writeToFile(GetScanAlertServlet.java:256)
at GetScanAlertServlet.checkAndCreateFile(GetScanAlertServlet.java:236)
at GetScanAlertServlet.checkAndCreateDir(GetScanAlertServlet.java:202)
at GetScanAlertServlet.processRequest(GetScanAlertServlet.java:135)
at GetScanAlertServlet.doPost(GetScanAlertServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
lter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrinc
ipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
yAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
e.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor
kerThread.java:112)
at java.lang.Thread.run(Unknown Source)
Though I do not see the problem right away, your code does not properly handle resource allocation/release. The problems do not have to be caused by manipulating large JSON blob.
I just observed you do not free your resources (you open files, but do not close them in finally blocks -- any reason not to?), you would probably do better with StringBuilder for string manipulation or just use some kind of existing library (apache commons (io, string)) to do it for you.
Scheduled executor service should be properly shutted down (maybe use something your container provides: Jboss thread pool).
To start I had to rewrite most of the code. I know it's bad practice on SO to do that, We are here to teach and help. Not do other peoples work. But I could really stand reading the code and made it hard to follow.
Here are the bullet points of issues I found
No finally clauses, So your FileWriter, 'FileReader, andBufferedReader` were never closed if an exception occurred.
Not using static where it could, path and file names never changed. Also your DateFormat never changed so moved that to static
Not sure why you were setting strings to null when the next line was getting it from the request parameters and if it was null changed it to an empty string anyway.
Not sure why you were converting dates to strings to compare them. Dates are comparable.
anyway here is the code hope it helps
public class GetScanAlertServlet extends HttpServlet
{
private static final String PATH = "D:\\Mobile_scan_alerts";
private static final String STOCK_FILE_NAME = "stock.txt";
private static final String FO_FILE_NAME = "fo.txt";
private static final String EMPTY = "";
private static final DateFormat FORMAT_WITHOUT_SEC = new SimpleDateFormat("yyyy/MM/dd HH:mm");
// For average time of received data
private static float SUM = 0;
private static float S_SUM = 0;
private static float FO_SUM = 0;
private static float COUNT = 0;
private static float S_COUNT = 0;
private static float FO_COUNT = 0;
private static Logger LOGGER = null;
private String scanType;
private String stockData;
private String foData;
#Override
public void init()
{
LOGGER = MyLogger.getScanAlertLogger();
if (LOGGER == null)
{
MyLogger.createLog();
LOGGER = MyLogger.getScanAlertLogger();
}
}
/**
* Returns a short description of the servlet.
*
* #return a String containing servlet description
*/
#Override
public String getServletInfo()
{
return "Short description";
}
/**
* Handles the HTTP <code>GET</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
String strScan = EMPTY;
try
{
scanType = getRequestParameter(request, "type");
if (scanType.length() > 0)
{
if (scanType.equalsIgnoreCase("s"))
{
stockData = getRequestParameter(request, "scanData");
}
else
{
foData = getRequestParameter(request, "scanData");
}
}
//format: yyyy/MM/dd HH:mm
Date asofDate = FORMAT_WITHOUT_SEC.parse(getRequestParameter(request, "asof"));
Date currDate = new Date();
currDate.setSeconds(0);
System.out.println(asofDate.toString() + " || " + currDate.toString());
if (asofDate.compareTo(currDate) >= 0)
{
if (scanType != null && scanType.length() > 0)
{
checkAndCreateDir();
strScan =
scanType.equalsIgnoreCase("s") ? "Stock Data Recieved at " + currDate :
"FO Data Recieved at " + currDate;
}
else
{
strScan = "JSON of scan data not received properly at " + currDate;
}
}
else
{
strScan = "GSAS: " + scanType + ": Received Expired Data of " + asofDate.toString()
+ " at " + currDate.toString();
System.out.println(strScan);
}
}
catch (Exception ex)
{
strScan = "Mobile server issue for receiving scan data";
LOGGER.error("GetScanAlertServlet: processRequest(): Exception: " + ex.toString());
}
finally
{
LOGGER.info("GetScanAlertServlet: " + strScan);
out.println(strScan);
out.close();
}
}
private void checkAndCreateDir()
{
try
{
File dir = new File(PATH);
if (!dir.exists())
{
dir.mkdir();
}
File file = null;
SUM += new Date().getSeconds();
COUNT++;
LOGGER.info("Total Average Time: " + (SUM / COUNT));
if (scanType.equalsIgnoreCase("s"))
{ //For Stock
S_SUM += new Date().getSeconds();
S_COUNT++;
LOGGER.info("Stock Average Time: " + (S_SUM / S_COUNT));
file = new File(PATH + System.lineSeparator() + STOCK_FILE_NAME);
}
else if (scanType.equalsIgnoreCase("fo"))
{ //For FO
FO_SUM += new Date().getSeconds();
FO_COUNT++;
LOGGER.info("FO Average Time: " + (FO_SUM / FO_COUNT));
file = new File(PATH + System.lineSeparator() + FO_FILE_NAME);
}
checkAndCreateFile(file);
}
catch (Exception e)
{
//System.out.println("GSAS: Exception-2: "+e);
LOGGER.error("GetScanAlertServlet: checkAndCreateDir(): Exception: " + e.toString());
}
}
private void checkAndCreateFile(File file)
{
try
{
if(!file.exists())
{
file.createNewFile();
}
writeToFile(file);
}
catch (Exception e)
{
LOGGER.error("GetScanAlertServlet: checkAndCreateFile(): Exception: " + e.toString());
}
}
private void writeToFile(File file) throws IOException
{
String data = EMPTY;
if (scanType.equalsIgnoreCase("s"))
{ //For Stock
if (stockData == null)
{
stockData = readFromFile(PATH + System.lineSeparator() + STOCK_FILE_NAME);
}
data = stockData;
}
else if (scanType.equalsIgnoreCase("fo"))
{ //For FO
if (foData == null)
{
foData = readFromFile(PATH + System.lineSeparator() + FO_FILE_NAME);
}
data = foData;
}
FileWriter fileWriter = null;
try
{
if (data != null && data.length() > 0)
{
fileWriter = new FileWriter(file);
fileWriter.write(data.toString());
}
else
{
System.out.println("GSAS: Data is null/empty string");
LOGGER.info("GSAS: Data is null or empty string");
}
}
catch (Exception e)
{
LOGGER.info("GetScanAlertServlet: writeToFile(): Exception: " + e.toString());
}
finally
{
if (fileWriter != null)
{
fileWriter.flush();
fileWriter.close();
}
}
}
private String readFromFile(String fileName) throws IOException
{
String fileContent = EMPTY;
FileReader fr = null;
BufferedReader br = null;
try
{
File file = new File(fileName);
if (file.exists())
{
fr = new FileReader(file);
br = new BufferedReader(fr);
String temp;
while ((temp = br.readLine()) != null)
{
fileContent += temp;
}
}
else
{
System.out.println("GSAS: File not exists to read");
LOGGER.info("GetScanAlertServlet: File not exists to read");
}
}
catch (Exception e)
{
LOGGER.error("GetScanAlertServlet: readFromFile(): Exception: " + e.toString());
}
finally
{
if (fr != null)
{
fr.close();
}
if (br != null)
{
br.close();
}
}
return fileContent;
}
private String getRequestParameter(HttpServletRequest request, String parameter)
{
String str = request.getParameter(parameter);
return str == null ? EMPTY : str.trim();
}
}

"<identifier> expected" when creating class from template

I have a bot I am creating that can take input from an IRC channel to create new classes for the bot to use when running. However, when it tries to compile the class, it results in an "identifier expected" error at the class name. However, if I type up a class which is identical to that created by the bot using the template, it compiles without issue. Below are the 3 methods used for this process:
//Create basic command
public static int writeBasicCommand(String trigger, String output, boolean edit) {
int success = 0, existenceError = 1, unknownError = 2;
try {
String filePath = "C:/commands/" + trigger + ".java"; //Location for new class
File file = new File(filePath);
//Check if command exists
if (file.exists()) {
if(!edit) {
return existenceError;
}
} else if(edit) {
return existenceError;
}
//Grab and modify template
String template = readFile("C:/template.txt");
String namedCom = template.replace("--COMMANDNAME--", trigger);
String content = namedCom.replace("--COMMANDRESULT--", "\"" + output + "\"");
//Write command
WriteFile(content, file, false);
if (Compile(filePath)==true) {
System.out.println("Done");
return success;
} else {
return unknownError;
}
} catch (IOException e) {
e.printStackTrace();
return unknownError;
}
}
//Compile new commands
public static boolean Compile(String fileToCompile) {
System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_11");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, fileToCompile);
if(compilationResult == 0) {
System.out.println("Compilation is successful");
return true;
} else {
System.out.println("Compilation Failed");
if ((new File(fileToCompile).exists())) {
new File(fileToCompile).delete();
}
return false;
}
}
//Write to a file
public static boolean WriteFile(String fileContents, File destination, boolean append) {
try {
// if file doesnt exist, then create it
if (!destination.exists()) {
destination.createNewFile();
}
FileWriter fw = new FileWriter(destination.getAbsoluteFile(), append);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fileContents);
bw.close();
fw.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

sound file not playing

I am writing a game for a class and having trouble adding sound and images. We were given the framework from Andrew Davison's Killer Game Programming book to work with, and I feel like I am copying it exactly, but I am unable to play a sound file.
This is the SDLLoader class:
package zombieCity;
/* SDLLoader stores a collection of SDLInfo objects
in a HashMap whose keys are their names.
The name and filename for a line is obtained from a sounds
information file which is loaded when SDLLoader is created.
The information file is assumed to be in Sounds/.
SDLLoader allows a specified clip to be played, stopped,
resumed, looped. A SoundsWatcher can be attached to a clip.
All of this functionality is handled in the SDLInfo object;
SDLLoader simply redirects the method call to the right SDLInfo.
It is possible for many lines to play at the same time, since
each SDLInfo object is responsible for playing its clip.
*/
import java.util.*;
import java.io.*;
import javax.print.DocFlavor.URL;
public class SDLLoader
{
private final static String SOUND_DIR = "Sounds/";
private HashMap<String, SDLInfo> sdlMap;
/* The key is the clip 'name', the object (value)
is a SDLInfo object */
public SDLLoader(String soundsFnm)
{ sdlMap = new HashMap<String, SDLInfo>();
loadSoundsFile(soundsFnm);
}
public SDLLoader()
{ sdlMap = new HashMap<String, SDLInfo>(); }
private void loadSoundsFile(String soundsFnm)
/* The file format are lines of:
<name> <filename> // a single sound file
and blank lines and comment lines.
*/
{
String sndsFNm = SOUND_DIR + soundsFnm;
System.out.println("Reading file: " + sndsFNm);
try {
java.net.URL inFile = this.getClass().getResource("/Sounds/SDLInfo.txt");
System.out.println("url " + inFile);
InputStreamReader in = new InputStreamReader(inFile.openStream());
BufferedReader br = new BufferedReader(in);
StringTokenizer tokens;
String line, name, fnm;
while((line = br.readLine()) != null) {
if (line.length() == 0) // blank line
continue;
if (line.startsWith("//")) // comment
continue;
tokens = new StringTokenizer(line);
if (tokens.countTokens() != 2)
System.out.println("Wrong no. of arguments for " + line);
else {
name = tokens.nextToken();
fnm = tokens.nextToken();
load(name, fnm);
}
}
br.close();
}
catch (IOException e)
{ System.out.println("Error reading file: " + sndsFNm);
System.exit(1);
}
} // end of loadSoundsFile()
// ----------- manipulate a particular clip --------
public void load(String name, String fnm)
// create a SDLInfo object for name and store it
{
if (sdlMap.containsKey(name))
System.out.println( "Error: " + name + "already stored");
else {
sdlMap.put(name, new SDLInfo(name, fnm) );
System.out.println("-- " + name + "/" + fnm);
}
} // end of load()
public void close(String name)
// close the specified clip
{ SDLInfo si = (SDLInfo) sdlMap.get(name);
if (si == null)
System.out.println( "Error: " + name + "not stored");
else
si.close();
} // end of close()
public void play(String name, boolean toLoop)
// play (perhaps loop) the specified clip
{ SDLInfo si = (SDLInfo) sdlMap.get(name);
if (si == null)
System.out.println( "Error: " + name + "not stored");
else
si.beginPlayback(toLoop);
} // end of play()
public void stop(String name)
// stop the clip, resetting it to the beginning
{ SDLInfo si = (SDLInfo) sdlMap.get(name);
if (si == null)
System.out.println( "Error: " + name + "not stored");
else
si.stop();
} // end of stop()
// -------------------------------------------------------
public void setWatcher(String name, SoundsWatcher sw)
/* Set up a watcher for the clip. It will be notified when
the clip loops or stops. */
{ SDLInfo si = (SDLInfo) sdlMap.get(name);
if (si == null)
System.out.println( "Error: " + name + "not stored");
else
si.setWatcher(sw);
} // end of setWatcher()
} // end of ClipsLoader class
stack trace
fps: 100; period: 10 ms
Reading file: Sounds/SDLInfo.txt
url file:/C:/Documents%20and%20Settings/MyName/Desktop/java%20programs/Zombie%20City/bin/Sounds/SDLInfo.txt
Exception in thread "main" java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at zombieCity.SDLInfo.loadLine(SDLInfo.java:35)
at zombieCity.SDLInfo.<init>(SDLInfo.java:27)
at zombieCity.SDLLoader.load(SDLLoader.java:95)
at zombieCity.SDLLoader.loadSoundsFile(SDLLoader.java:73)
at zombieCity.SDLLoader.<init>(SDLLoader.java:38)
at zombieCity.FlyingHero.simpleInitialize(FlyingHero.java:144)
at zombieCity.Frame.<init>(Frame.java:68)
at zombieCity.FlyingHero.<init>(FlyingHero.java:80)
at zombieCity.FlyingHero.main(FlyingHero.java:376)
error was being caught in this class:
package zombieCity;
/* Load a line, which can be played, stopped, resumed, looped.
An object implementing the SoundsWatcher interface
can be notified when the line loops or stops.
*/
import java.io.*;
import javax.sound.sampled.*;
public class SDLInfo implements LineListener, Runnable
{
private final static String SOUND_DIR = "Sounds/";
private String name, filename;
private SourceDataLine line = null;
private boolean isLooping = false;
private SoundsWatcher watcher = null;
private Thread soundPlayer;
public SDLInfo(String nm, String fnm)
{ name = nm;
filename = SOUND_DIR + fnm;
loadLine(filename);
} // end of SDLInfo()
private void loadLine(String fnm)
{
try {
// link an audio stream to the sound line's file
AudioInputStream stream = AudioSystem.getAudioInputStream(
getClass().getResource(fnm) );
AudioFormat format = stream.getFormat();
// convert ULAW/ALAW formats to PCM format
if ( (format.getEncoding() == AudioFormat.Encoding.ULAW) ||
(format.getEncoding() == AudioFormat.Encoding.ALAW) ) {
AudioFormat newFormat =
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(), true); // big endian
// update stream and format details
stream = AudioSystem.getAudioInputStream(newFormat, stream);
System.out.println("Converted Audio format: " + newFormat);
format = newFormat;
}
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
// make sure sound system supports data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Unsupported Line File: " + fnm);
return;
}
// get clip line resource
line = (SourceDataLine) AudioSystem.getLine(info);
// listen to line for events
line.addLineListener(this);
line.open(format); // open the sound file as a line
stream.close(); // we're done with the input stream
} // end of try block
catch (UnsupportedAudioFileException audioException) {
System.out.println("Unsupported audio file: " + fnm);
}
catch (LineUnavailableException noLineException) {
System.out.println("No audio line available for : " + fnm + " " + noLineException);
}
catch (IOException ioException) {
System.out.println("Could not read: " + fnm);
}
// catch (Exception e) {
// System.out.println("Problem with " + fnm);
// }
} // end of loadLine()
public void update(LineEvent lineEvent){}
// update() not used
public void close()
{ if (line != null) {
line.stop();
line.close();
}
}
public void beginPlayback(boolean toLoop)
{
if (line != null){
isLooping = toLoop;
if (soundPlayer == null) {
soundPlayer = new Thread(this);
soundPlayer.start();
}
}
}
public void play()
{
try{
if (line != null) {
int numRead = 0;
byte[] buffer = new byte[line.getBufferSize()];
AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource(filename) );
line.start();
// read and play chunks of the audio
int offset;
while ((numRead = stream.read(buffer,0,buffer.length)) >= 0) {
offset = 0;
while (offset < numRead)
offset += line.write(buffer, offset, numRead-offset);
}
// wait until all data is played, then stop the line
stream.close();
line.drain();
line.stop();
}
}
catch (IOException ioException) {
System.out.println("Could not read: " + filename);
}
catch (UnsupportedAudioFileException audioException) {
System.out.println("Unsupported audio file: " + filename);
}
} //end of play()
public void run()
{
do{ play();}while(isLooping);
}
public void stop()
// stop and reset line to its start
{ if (line != null) {
isLooping = false;
line.drain();
line.stop();
}
}
public void setWatcher(SoundsWatcher sw)
{ watcher = sw; }
// -------------- other access methods -------------------
public String getName()
{ return name; }
} // end of ClipInfo class
Have you tried it with a .wav file
if you need to comvert convert at
convert here
try adding the full sound file directory at
private final static String SOUND_DIR = "Sounds/";
Example dir
"C:/Stuff/Sounds/soundfile.wav
This May help you
here

Categories