Ujjwal's Blog
Programming Blog
Tuesday, May 21, 2013
Windows Master Control Panel Shortcut aka ‘GOD MODE’
Wednesday, April 10, 2013
Windows authentication and fake/local domains defined in your host file
I like to create my own local domains when creating a website on my computer as it’s both easier to work with and to remember than using the default site with a lot of virtual directories/applications. However if you want to use windows authentication on a fake domain like this you will run into problems. If you create your own fake domain on a computer using the host file (for instance www.test.local) and want to enable windows authentication on the site this won’t work out of the box. Here are some pointers on how to get this to work properly.
1. Edit C:\windows\system32\drivers\etc\hosts
You might have to change the security settings on this file to be able to edit this file.
Fill in the hostname field with the domain entered in your hosts file like below:
3. Configure your site to use Windows Authentication.
This is done in the Internet Information Server Manager by clicking on your site and then the Authentication icon. Disable all options but Windows Authentication and if needed ASP.NET Impersonation.
Even if you’ve done everything above which are the typical steps to enable windows authentication on a normal web site using a proper domain it might not/won’t work using a host name specified domain.
Cheers,
Friday, March 15, 2013
Compress Images on upload
Recently i wanted to compress images when uploading using Postgresql and hibernate. I implemented below code :
Image compressor utility code below ::
package com.utils;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import com.sun.media.jai.codec.SeekableStream;
/**
*
* @author Ujjwal Soni
*
*/
public class ImageCompressor {
public byte[] compressFile(byte[] content, String fileName)
throws FileNotFoundException {
BufferedImage input = null;
InputStream ips = null;
ByteArrayOutputStream baout = new ByteArrayOutputStream();
try {
ips = new ByteArrayInputStream(content);
if (fileName.endsWith(".jpg") || fileName.endsWith(".JPG") || fileName.endsWith(".jpeg") || fileName.endsWith(".JPEG")) {
input = ImageIO.read(ips);
} else if (fileName.endsWith(".gif") || fileName.endsWith(".GIF")) {
input = ImageIO.read(ips);
} else if (fileName.endsWith(".bmp") || fileName.endsWith(".BMP")) {
// Wrap the InputStream in a SeekableStream.
SeekableStream s = SeekableStream.wrapInputStream(ips, false);
// Create the ParameterBlock and add the SeekableStream to it.
ParameterBlock pb = new ParameterBlock();
pb.add(s);
// Perform the BMP operation
RenderedOp img1 = JAI.create("BMP", pb);
input = getBufferedImage(img1.getAsBufferedImage());
} else if (fileName.endsWith(".png") || fileName.endsWith(".PNG")) {
SeekableStream s = SeekableStream.wrapInputStream(ips, false);
// Create the ParameterBlock and add the SeekableStream to it.
ParameterBlock pb = new ParameterBlock();
pb.add(s);
// Perform the PNG operation
RenderedOp img1 = JAI.create("PNG", pb);
input = getBufferedImage(img1.getAsBufferedImage());
}
if (input == null)
return null;
// Get Writer and set compression
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float values[] = iwp.getCompressionQualityValues();
System.out.println("compression Quality >> "+values[2]);
iwp.setCompressionQuality(0.2f);
//BufferedImage image = ImageIO.read(ips);
IIOImage image1 = new IIOImage(input, null, null);
writer.setOutput(new MemoryCacheImageOutputStream(baout));
// writer.write(null, new IIOImage(image, null, null), iwp);
writer.write(null, image1, iwp);
input.flush();
writer.dispose();
writer = null;
image1 = null;
input = null;
}
} catch (FileNotFoundException finfExcp) {
System.out.println(finfExcp);
} catch (IOException ioExcp) {
System.out.println(ioExcp);
}
return baout.toByteArray();
}
private BufferedImage getBufferedImage(Image img) {
// if the image is already a BufferedImage, cast and return it
// if ((img instanceof BufferedImage)) {
// return (BufferedImage) img;
// }
// otherwise, create a new BufferedImage and draw the original
// image on it
int w = img.getWidth(null);
int h = img.getHeight(null);
int thumbWidth = 330;
int thumbHeight = 250;
// if width is less than 330 keep the width as it is.
if (w < thumbWidth)
thumbWidth = w;
// if height is less than 250 keep the height as it is.
if (h < thumbHeight)
thumbHeight = h;
// if less than 330*250 then do not compress
if (w > 330 || h > 250) {
double imageRatio = (double) w / (double) h;
double thumbRatio = (double) thumbWidth / (double) thumbWidth;
if (thumbRatio < imageRatio) {
thumbHeight = (int) (thumbWidth / imageRatio);
} else {
thumbWidth = (int) (thumbHeight * imageRatio);
}
}
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage bi = new BufferedImage(thumbWidth, thumbHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(img, 0, 0, thumbWidth, thumbHeight, null);
g2d.dispose();
return bi;
}
}
for(FileBean fileBean: fileList){
if(fileBean.getActualFile()!=null && fileBean.getActualFile().getFileSize()>0)
{
//byte[] bFile = new byte[ fileBean.getActualFile().getFileSize()];
byte[] bFile = fileBean.getFileContent();
fileBean.getActualFile().getInputStream().read(bFile);
MyDocumentImage mydocimage=new MyDocumentImage();
//MyDocumentImageId mydocimageId=new MyDocumentImageId();
mydocimage.setMyDocImage(new MyDocImageType(fileType));
if (fileBean.getActualFile().getFileName().endsWith(".jpg") || fileBean.getActualFile().getFileName().endsWith(".JPG") || fileBean.getActualFile().getFileName().endsWith(".jpeg") || fileBean.getActualFile().getFileName().endsWith(".JPEG") || fileBean.getActualFile().getFileName().endsWith(".gif") || fileBean.getActualFile().getFileName().endsWith(".GIF") || fileBean.getActualFile().getFileName().endsWith(".bmp") || fileBean.getActualFile().getFileName().endsWith(".BMP") || fileBean.getActualFile().getFileName().endsWith(".png") || fileBean.getActualFile().getFileName().endsWith(".PNG") && fileBean.getActualFile().getFileSize()>300000)
{
mydocimage.setDocContent(img.compressFile(bFile, fileBean.getActualFile().getFileName()));
}
else
{
mydocimage.setDocContent(bFile);
}
mydocimage.setDocCreatedBy(userId);
mydocimage.setDocCreatedDate(new Timestamp(System.currentTimeMillis()));
mydocimage.setSdocLatest(true);
mydocimage.setSdocMimeType(fileBean.getActualFile().getContentType());
mydocimage.setSdocFileName(fileBean.getActualFile().getFileName());
mydocimage.setDocActive("Y");
mydocimage.setDocVersion(1);
//String docSeq = commonDAO.getNextSequence("my_doc.seq");
//mydocimage.setSCode(Integer.valueOf(sdocSeq));
//mydocimage.setSdocDtlCode(fileType);
//mydocimage.setSMewCode(staStall.getId().getStaBusCode());
//mydocimage.setSCode(staStall.getId().getStaCode());
//mydocimage.setId(mydocimageId);
mydocimage.setDs(dStp);
myDocList.getMyDocumentImages().add(mydocimage);
}
}
Thats it, above code will compress images of particular size. Email me if you need source code or guidance on this.
Cheers,
Ujjwal Soni
Thursday, February 28, 2013
Multiple skype instances on MACBOOK
I have two skype id's and i had to login both of them simentaneously, so i opened up terminal and executed below command.
sudo /Applications/Skype.app/Contents/MacOS/Skype /secondary
Cheers,
Ujjwal Soni
Absolute Beginner's Guide to Bit Shifting
The bit shifting operators do exactly what their name implies. They shift bits. Here's a brief (or not-so-brief) introduction to the different shift operators.
The Operators
>>is the arithmetic (or signed) right shift operator.>>>is the logical (or unsigned) right shift operator.<<is the left shift operator, and meets the needs of both logical and arithmetic shifts.
All of these operators can be applied to integer values (int, long, possibly short and byte or char). In some languages, applying the shift operators to any datatype smaller than int automatically resizes the operand to be an int.
Note that <<< is not an operator, because it would be redundant. Also note that C and C++ do not distingiush between the right shift operators. They provide only the >> operator, and the shifting behavior is implementation defined.
Left shift (<<)
Integers are stored, in memory, as a series of bits. For example, the number 6 stored as a 32-bit int would be:
00000000 00000000 00000000 00000110
Shifting this bit pattern to the left one position (6 << 1) would result in the number 12:
00000000 00000000 00000000 00001100
As you can see, the digits have shifted to the left by one position, and the last digit on the right is filled with a zero. You might also note that shifting left is equivalent to multiplication by powers of 2. So 6 << 1 is equivalent to 6 * 2, and 6 << 3 is equivalent to 6 * 8. A good optimizing compiler will substitute shifts for multiplications when possible.
Non-circular shifting
Please note that these are not circular shifts. Shifting this value to the left by one position (3,758,096,384 << 1):
11100000 00000000 00000000 00000000
results in 3,221,225,472:
11000000 00000000 00000000 00000000
The digit that gets shifted "off the end" is lost. It does not wrap around.
Logical right shift (>>>)
A logical right shift is the converse to the left shift. Rather than moving bits to the left, they simply move to the right. For example, shifting the number 12:
00000000 00000000 00000000 00001100
to the right by one position (12 >>> 1) will get back our original 6:
00000000 00000000 00000000 00000110
So we see that shifting to the right is equivalent to division by powers of 2.
Lost bits are gone
However, a shift cannot reclaim "lost" bits. For example, if we shift this pattern:
00111000 00000000 00000000 00000110
to the left 4 positions (939,524,102 << 4), we get 2,147,483,744:
10000000 00000000 00000000 01100000
and then shifting back ((939,524,102 << 4) >>> 4) we get 134,217,734:
00001000 00000000 00000000 00000110
We cannot get back our original value once we have lost bits.
Arithmetic right shift (>>)
The arithmetic right shift is exactly like the logical right shift, except instead of padding with zero, it pads with the most significant bit. This is because the most significant bit is the sign bit, or the bit that distinguishes positive and negative numbers. By padding with the most significant bit, the arithmetic right shift is sign-preserving.
For example, if we interpret this bit pattern as a negative number:
10000000 00000000 00000000 01100000
we have the number -2,147,483,552. Shifting this to the right 4 positions with the arithmetic shift (-2,147,483,552 >> 4) would give us:
11111000 00000000 00000000 00000110
or the number -134,217,722.
So we see that we have preserved the sign of our negative numbers by using the arithmetic right shift, rather than the logical right shift. And once again, we see that we are performing division by powers of 2.
My first AVR LED Blink program in C
I recieved my AVR programmer device last week so, i wrote my first LED blinking program in C (AVR). I used ATMEGA328P-PU chip. Earlier i have assembled my own arduino board so i used the same board for AVR by erasing the bootloader. below is my code
#include <avr/io.h>
#include <util/delay.h>
/*
@author Ujjwal Soni
Connect a LED between pin 13 and GND.
*/
void delay()
{
unsigned char counter = 0;
while (counter != 50)
{
/* wait (30000 x 4) cycles = wait 120000 cycles */
_delay_loop_2(30000);
counter++;
}
}
int main(void)
{
/* Initialization, set PB5 (arduino digital pin 13) as output */
DDRB |= (1<<PB5);
while (1)
{
PORTB |= (1<<PB5); //arduino digital pin 5 -> 5V
delay();
PORTB &= ~(1<<PB5); //arduino digital pin 5 -> GND
delay();
}
return 0;
}
Please note that my LED is connected on PIN13.
I will be also writing pushbutton code which i will be posting here soon.
Cheers,
Ujjwal Soni
Experience with Joomla
I used Joomla for developing a website. I found it very odd and tricky. I have also used liferay which is quite easy and straight forward. You can share your experiences by commenting on my blog.
Thanks,
Ujjwal Soni




