Clearly its your mistake. You should be driving with some distance to the car infront of you.
And even of some one applies sudden brakes you should have enough time and space to react and stop the car.
I use to do the same mistake but once on islamabad highway the corolla infront of me braked really hard and i manged to stop my charade just inches from it.
From that day onwards its been almost 6 months i always keep safe distance and i have avoided many of these small accidents not only saved my car but also saved the idiots behind me just 2 feet away